23 |
|
/** |
24 |
|
* exchange exchanges objects across two threads |
25 |
|
*/ |
26 |
< |
public void testExchange() throws InterruptedException { |
26 |
> |
public void testExchange() { |
27 |
|
final Exchanger e = new Exchanger(); |
28 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
28 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
29 |
|
public void realRun() throws InterruptedException { |
30 |
|
assertSame(one, e.exchange(two)); |
31 |
|
assertSame(two, e.exchange(one)); |
32 |
|
}}); |
33 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
33 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
34 |
|
public void realRun() throws InterruptedException { |
35 |
|
assertSame(two, e.exchange(one)); |
36 |
|
assertSame(one, e.exchange(two)); |
37 |
|
}}); |
38 |
|
|
39 |
< |
t1.start(); |
40 |
< |
t2.start(); |
41 |
< |
t1.join(); |
42 |
< |
t2.join(); |
39 |
> |
awaitTermination(t1); |
40 |
> |
awaitTermination(t2); |
41 |
|
} |
42 |
|
|
43 |
|
/** |
44 |
|
* timed exchange exchanges objects across two threads |
45 |
|
*/ |
46 |
< |
public void testTimedExchange() throws InterruptedException { |
46 |
> |
public void testTimedExchange() { |
47 |
|
final Exchanger e = new Exchanger(); |
48 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
48 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
49 |
|
public void realRun() throws Exception { |
50 |
< |
assertSame(one, e.exchange(two, SHORT_DELAY_MS, MILLISECONDS)); |
51 |
< |
assertSame(two, e.exchange(one, SHORT_DELAY_MS, MILLISECONDS)); |
50 |
> |
assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS)); |
51 |
> |
assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS)); |
52 |
|
}}); |
53 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
53 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
54 |
|
public void realRun() throws Exception { |
55 |
< |
assertSame(two, e.exchange(one, SHORT_DELAY_MS, MILLISECONDS)); |
56 |
< |
assertSame(one, e.exchange(two, SHORT_DELAY_MS, MILLISECONDS)); |
55 |
> |
assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS)); |
56 |
> |
assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS)); |
57 |
|
}}); |
58 |
|
|
59 |
< |
t1.start(); |
60 |
< |
t2.start(); |
63 |
< |
t1.join(); |
64 |
< |
t2.join(); |
59 |
> |
awaitTermination(t1); |
60 |
> |
awaitTermination(t2); |
61 |
|
} |
62 |
|
|
63 |
|
/** |
64 |
|
* interrupt during wait for exchange throws IE |
65 |
|
*/ |
66 |
< |
public void testExchange_InterruptedException() throws InterruptedException { |
66 |
> |
public void testExchange_InterruptedException() { |
67 |
|
final Exchanger e = new Exchanger(); |
68 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
68 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
69 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
70 |
|
public void realRun() throws InterruptedException { |
71 |
+ |
threadStarted.countDown(); |
72 |
|
e.exchange(one); |
73 |
|
}}); |
74 |
|
|
75 |
< |
t.start(); |
78 |
< |
delay(SHORT_DELAY_MS); |
75 |
> |
await(threadStarted); |
76 |
|
t.interrupt(); |
77 |
< |
t.join(); |
77 |
> |
awaitTermination(t); |
78 |
|
} |
79 |
|
|
80 |
|
/** |
81 |
|
* interrupt during wait for timed exchange throws IE |
82 |
|
*/ |
83 |
< |
public void testTimedExchange_InterruptedException() throws InterruptedException { |
83 |
> |
public void testTimedExchange_InterruptedException() { |
84 |
|
final Exchanger e = new Exchanger(); |
85 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
85 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
86 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
87 |
|
public void realRun() throws Exception { |
88 |
< |
e.exchange(null, SMALL_DELAY_MS, MILLISECONDS); |
88 |
> |
threadStarted.countDown(); |
89 |
> |
e.exchange(null, LONG_DELAY_MS, MILLISECONDS); |
90 |
|
}}); |
91 |
|
|
92 |
< |
t.start(); |
94 |
< |
delay(SHORT_DELAY_MS); |
92 |
> |
await(threadStarted); |
93 |
|
t.interrupt(); |
94 |
< |
t.join(); |
94 |
> |
awaitTermination(t); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* timeout during wait for timed exchange throws TOE |
98 |
> |
* timeout during wait for timed exchange throws TimeoutException |
99 |
|
*/ |
100 |
< |
public void testExchange_TimeOutException() throws InterruptedException { |
100 |
> |
public void testExchange_TimeoutException() { |
101 |
|
final Exchanger e = new Exchanger(); |
102 |
< |
Thread t = new ThreadShouldThrow(TimeoutException.class) { |
102 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
103 |
|
public void realRun() throws Exception { |
104 |
< |
e.exchange(null, SHORT_DELAY_MS, MILLISECONDS); |
105 |
< |
}}; |
104 |
> |
long timeoutMillis = SHORT_DELAY_MS; |
105 |
> |
long startTime = System.nanoTime(); |
106 |
> |
try { |
107 |
> |
e.exchange(null, timeoutMillis, MILLISECONDS); |
108 |
> |
shouldThrow(); |
109 |
> |
} catch (TimeoutException success) { |
110 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
111 |
> |
} |
112 |
> |
}}); |
113 |
|
|
114 |
< |
t.start(); |
110 |
< |
t.join(); |
114 |
> |
awaitTermination(t); |
115 |
|
} |
116 |
|
|
117 |
|
/** |
119 |
|
*/ |
120 |
|
public void testReplacementAfterExchange() throws InterruptedException { |
121 |
|
final Exchanger e = new Exchanger(); |
122 |
< |
Thread t1 = new Thread(new CheckedInterruptedRunnable() { |
122 |
> |
final CountDownLatch exchanged = new CountDownLatch(2); |
123 |
> |
final CountDownLatch interrupted = new CountDownLatch(1); |
124 |
> |
Thread t1 = newStartedThread(new CheckedInterruptedRunnable() { |
125 |
|
public void realRun() throws InterruptedException { |
126 |
|
assertSame(two, e.exchange(one)); |
127 |
+ |
exchanged.countDown(); |
128 |
|
e.exchange(two); |
129 |
|
}}); |
130 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
130 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
131 |
|
public void realRun() throws InterruptedException { |
132 |
|
assertSame(one, e.exchange(two)); |
133 |
< |
delay(SMALL_DELAY_MS); |
133 |
> |
exchanged.countDown(); |
134 |
> |
interrupted.await(); |
135 |
|
assertSame(three, e.exchange(one)); |
136 |
|
}}); |
137 |
< |
Thread t3 = new Thread(new CheckedRunnable() { |
137 |
> |
Thread t3 = newStartedThread(new CheckedRunnable() { |
138 |
|
public void realRun() throws InterruptedException { |
139 |
< |
delay(SMALL_DELAY_MS); |
139 |
> |
interrupted.await(); |
140 |
|
assertSame(one, e.exchange(three)); |
141 |
|
}}); |
142 |
|
|
143 |
< |
t1.start(); |
136 |
< |
t2.start(); |
137 |
< |
t3.start(); |
138 |
< |
delay(SHORT_DELAY_MS); |
143 |
> |
await(exchanged); |
144 |
|
t1.interrupt(); |
145 |
< |
t1.join(); |
146 |
< |
t2.join(); |
147 |
< |
t3.join(); |
145 |
> |
interrupted.countDown(); |
146 |
> |
awaitTermination(t1); |
147 |
> |
awaitTermination(t2); |
148 |
> |
awaitTermination(t3); |
149 |
|
} |
150 |
|
|
151 |
|
} |