6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
+ |
import java.util.concurrent.CountDownLatch; |
12 |
+ |
import java.util.concurrent.Exchanger; |
13 |
+ |
import java.util.concurrent.TimeoutException; |
14 |
+ |
|
15 |
+ |
import junit.framework.Test; |
16 |
+ |
import junit.framework.TestSuite; |
17 |
+ |
|
18 |
|
public class ExchangerTest extends JSR166TestCase { |
19 |
|
|
20 |
|
public static void main(String[] args) { |
21 |
< |
junit.textui.TestRunner.run(suite()); |
21 |
> |
main(suite(), args); |
22 |
|
} |
23 |
|
public static Test suite() { |
24 |
|
return new TestSuite(ExchangerTest.class); |
105 |
|
final Exchanger e = new Exchanger(); |
106 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
107 |
|
public void realRun() throws Exception { |
104 |
– |
long timeoutMillis = SHORT_DELAY_MS; |
108 |
|
long startTime = System.nanoTime(); |
109 |
|
try { |
110 |
< |
e.exchange(null, timeoutMillis, MILLISECONDS); |
110 |
> |
e.exchange(null, timeoutMillis(), MILLISECONDS); |
111 |
|
shouldThrow(); |
112 |
< |
} catch (TimeoutException success) { |
113 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
111 |
< |
} |
112 |
> |
} catch (TimeoutException success) {} |
113 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
114 |
|
}}); |
115 |
|
|
116 |
|
awaitTermination(t); |
119 |
|
/** |
120 |
|
* If one exchanging thread is interrupted, another succeeds. |
121 |
|
*/ |
122 |
< |
public void testReplacementAfterExchange() throws InterruptedException { |
122 |
> |
public void testReplacementAfterExchange() { |
123 |
|
final Exchanger e = new Exchanger(); |
124 |
|
final CountDownLatch exchanged = new CountDownLatch(2); |
125 |
|
final CountDownLatch interrupted = new CountDownLatch(1); |
133 |
|
public void realRun() throws InterruptedException { |
134 |
|
assertSame(one, e.exchange(two)); |
135 |
|
exchanged.countDown(); |
136 |
< |
interrupted.await(); |
136 |
> |
await(interrupted); |
137 |
|
assertSame(three, e.exchange(one)); |
138 |
|
}}); |
139 |
|
Thread t3 = newStartedThread(new CheckedRunnable() { |
140 |
|
public void realRun() throws InterruptedException { |
141 |
< |
interrupted.await(); |
141 |
> |
await(interrupted); |
142 |
|
assertSame(one, e.exchange(three)); |
143 |
|
}}); |
144 |
|
|
145 |
|
await(exchanged); |
146 |
|
t1.interrupt(); |
145 |
– |
interrupted.countDown(); |
147 |
|
awaitTermination(t1); |
148 |
+ |
interrupted.countDown(); |
149 |
|
awaitTermination(t2); |
150 |
|
awaitTermination(t3); |
151 |
|
} |