1 |
dl |
1.1 |
/* |
2 |
dl |
1.5 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.15 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.6 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.22 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
|
11 |
jsr166 |
1.21 |
import java.util.concurrent.CountDownLatch; |
12 |
|
|
import java.util.concurrent.Exchanger; |
13 |
|
|
import java.util.concurrent.TimeoutException; |
14 |
jsr166 |
1.22 |
|
15 |
|
|
import junit.framework.Test; |
16 |
|
|
import junit.framework.TestSuite; |
17 |
dl |
1.1 |
|
18 |
dl |
1.2 |
public class ExchangerTest extends JSR166TestCase { |
19 |
jsr166 |
1.6 |
|
20 |
dl |
1.1 |
public static void main(String[] args) { |
21 |
jsr166 |
1.23 |
main(suite(), args); |
22 |
dl |
1.1 |
} |
23 |
|
|
public static Test suite() { |
24 |
jsr166 |
1.10 |
return new TestSuite(ExchangerTest.class); |
25 |
dl |
1.1 |
} |
26 |
|
|
|
27 |
dl |
1.3 |
/** |
28 |
dl |
1.4 |
* exchange exchanges objects across two threads |
29 |
dl |
1.3 |
*/ |
30 |
jsr166 |
1.17 |
public void testExchange() { |
31 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
32 |
jsr166 |
1.17 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
33 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
34 |
jsr166 |
1.13 |
assertSame(one, e.exchange(two)); |
35 |
|
|
assertSame(two, e.exchange(one)); |
36 |
jsr166 |
1.9 |
}}); |
37 |
jsr166 |
1.17 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
38 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
39 |
jsr166 |
1.13 |
assertSame(two, e.exchange(one)); |
40 |
|
|
assertSame(one, e.exchange(two)); |
41 |
jsr166 |
1.9 |
}}); |
42 |
|
|
|
43 |
jsr166 |
1.17 |
awaitTermination(t1); |
44 |
|
|
awaitTermination(t2); |
45 |
dl |
1.2 |
} |
46 |
|
|
|
47 |
dl |
1.3 |
/** |
48 |
dl |
1.4 |
* timed exchange exchanges objects across two threads |
49 |
dl |
1.3 |
*/ |
50 |
jsr166 |
1.17 |
public void testTimedExchange() { |
51 |
dl |
1.2 |
final Exchanger e = new Exchanger(); |
52 |
jsr166 |
1.17 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
53 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
54 |
jsr166 |
1.17 |
assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS)); |
55 |
|
|
assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS)); |
56 |
jsr166 |
1.9 |
}}); |
57 |
jsr166 |
1.17 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
58 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
59 |
jsr166 |
1.17 |
assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS)); |
60 |
|
|
assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS)); |
61 |
jsr166 |
1.9 |
}}); |
62 |
|
|
|
63 |
jsr166 |
1.17 |
awaitTermination(t1); |
64 |
|
|
awaitTermination(t2); |
65 |
dl |
1.1 |
} |
66 |
|
|
|
67 |
dl |
1.3 |
/** |
68 |
dl |
1.4 |
* interrupt during wait for exchange throws IE |
69 |
dl |
1.3 |
*/ |
70 |
jsr166 |
1.17 |
public void testExchange_InterruptedException() { |
71 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
72 |
jsr166 |
1.17 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
73 |
|
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
74 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
75 |
jsr166 |
1.17 |
threadStarted.countDown(); |
76 |
jsr166 |
1.9 |
e.exchange(one); |
77 |
|
|
}}); |
78 |
|
|
|
79 |
jsr166 |
1.17 |
await(threadStarted); |
80 |
jsr166 |
1.9 |
t.interrupt(); |
81 |
jsr166 |
1.17 |
awaitTermination(t); |
82 |
dl |
1.1 |
} |
83 |
|
|
|
84 |
dl |
1.3 |
/** |
85 |
dl |
1.4 |
* interrupt during wait for timed exchange throws IE |
86 |
dl |
1.3 |
*/ |
87 |
jsr166 |
1.17 |
public void testTimedExchange_InterruptedException() { |
88 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
89 |
jsr166 |
1.17 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
90 |
|
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
91 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
92 |
jsr166 |
1.17 |
threadStarted.countDown(); |
93 |
|
|
e.exchange(null, LONG_DELAY_MS, MILLISECONDS); |
94 |
jsr166 |
1.9 |
}}); |
95 |
|
|
|
96 |
jsr166 |
1.17 |
await(threadStarted); |
97 |
jsr166 |
1.9 |
t.interrupt(); |
98 |
jsr166 |
1.17 |
awaitTermination(t); |
99 |
dl |
1.1 |
} |
100 |
|
|
|
101 |
dl |
1.3 |
/** |
102 |
jsr166 |
1.17 |
* timeout during wait for timed exchange throws TimeoutException |
103 |
dl |
1.3 |
*/ |
104 |
jsr166 |
1.17 |
public void testExchange_TimeoutException() { |
105 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
106 |
jsr166 |
1.17 |
Thread t = newStartedThread(new CheckedRunnable() { |
107 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
108 |
jsr166 |
1.17 |
long startTime = System.nanoTime(); |
109 |
|
|
try { |
110 |
jsr166 |
1.20 |
e.exchange(null, timeoutMillis(), MILLISECONDS); |
111 |
jsr166 |
1.17 |
shouldThrow(); |
112 |
jsr166 |
1.20 |
} catch (TimeoutException success) {} |
113 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
114 |
jsr166 |
1.17 |
}}); |
115 |
jsr166 |
1.9 |
|
116 |
jsr166 |
1.17 |
awaitTermination(t); |
117 |
dl |
1.1 |
} |
118 |
dl |
1.4 |
|
119 |
|
|
/** |
120 |
|
|
* If one exchanging thread is interrupted, another succeeds. |
121 |
|
|
*/ |
122 |
jsr166 |
1.18 |
public void testReplacementAfterExchange() { |
123 |
dl |
1.4 |
final Exchanger e = new Exchanger(); |
124 |
jsr166 |
1.17 |
final CountDownLatch exchanged = new CountDownLatch(2); |
125 |
|
|
final CountDownLatch interrupted = new CountDownLatch(1); |
126 |
|
|
Thread t1 = newStartedThread(new CheckedInterruptedRunnable() { |
127 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
128 |
jsr166 |
1.13 |
assertSame(two, e.exchange(one)); |
129 |
jsr166 |
1.17 |
exchanged.countDown(); |
130 |
jsr166 |
1.13 |
e.exchange(two); |
131 |
jsr166 |
1.9 |
}}); |
132 |
jsr166 |
1.17 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
133 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
134 |
jsr166 |
1.13 |
assertSame(one, e.exchange(two)); |
135 |
jsr166 |
1.17 |
exchanged.countDown(); |
136 |
|
|
interrupted.await(); |
137 |
jsr166 |
1.13 |
assertSame(three, e.exchange(one)); |
138 |
jsr166 |
1.9 |
}}); |
139 |
jsr166 |
1.17 |
Thread t3 = newStartedThread(new CheckedRunnable() { |
140 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
141 |
jsr166 |
1.17 |
interrupted.await(); |
142 |
jsr166 |
1.13 |
assertSame(one, e.exchange(three)); |
143 |
jsr166 |
1.9 |
}}); |
144 |
|
|
|
145 |
jsr166 |
1.17 |
await(exchanged); |
146 |
jsr166 |
1.9 |
t1.interrupt(); |
147 |
jsr166 |
1.19 |
awaitTermination(t1); |
148 |
jsr166 |
1.17 |
interrupted.countDown(); |
149 |
|
|
awaitTermination(t2); |
150 |
|
|
awaitTermination(t3); |
151 |
dl |
1.4 |
} |
152 |
|
|
|
153 |
dl |
1.1 |
} |