1 |
|
/* |
2 |
|
* 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 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
8 |
+ |
import junit.framework.*; |
9 |
|
import java.util.ArrayList; |
10 |
|
import java.util.List; |
11 |
+ |
import java.util.concurrent.Phaser; |
12 |
+ |
import java.util.concurrent.CountDownLatch; |
13 |
+ |
import java.util.concurrent.TimeoutException; |
14 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
15 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
16 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
17 |
|
import java.util.concurrent.atomic.AtomicInteger; |
11 |
– |
import java.util.concurrent.*; |
12 |
– |
import junit.framework.Test; |
13 |
– |
import junit.framework.TestSuite; |
18 |
|
|
19 |
|
public class PhaserTest extends JSR166TestCase { |
20 |
|
|
26 |
|
return new TestSuite(PhaserTest.class); |
27 |
|
} |
28 |
|
|
29 |
+ |
private static final int maxParties = 65535; |
30 |
+ |
|
31 |
+ |
/** Checks state of unterminated phaser. */ |
32 |
+ |
protected void assertState(Phaser phaser, |
33 |
+ |
int phase, int parties, int unarrived) { |
34 |
+ |
assertEquals(phase, phaser.getPhase()); |
35 |
+ |
assertEquals(parties, phaser.getRegisteredParties()); |
36 |
+ |
assertEquals(unarrived, phaser.getUnarrivedParties()); |
37 |
+ |
assertEquals(parties - unarrived, phaser.getArrivedParties()); |
38 |
+ |
assertFalse(phaser.isTerminated()); |
39 |
+ |
} |
40 |
+ |
|
41 |
+ |
/** Checks state of terminated phaser. */ |
42 |
+ |
protected void assertTerminated(Phaser phaser, int maxPhase, int parties) { |
43 |
+ |
assertTrue(phaser.isTerminated()); |
44 |
+ |
int expectedPhase = maxPhase + Integer.MIN_VALUE; |
45 |
+ |
assertEquals(expectedPhase, phaser.getPhase()); |
46 |
+ |
assertEquals(parties, phaser.getRegisteredParties()); |
47 |
+ |
assertEquals(expectedPhase, phaser.register()); |
48 |
+ |
assertEquals(expectedPhase, phaser.arrive()); |
49 |
+ |
assertEquals(expectedPhase, phaser.arriveAndDeregister()); |
50 |
+ |
} |
51 |
+ |
|
52 |
+ |
protected void assertTerminated(Phaser phaser, int maxPhase) { |
53 |
+ |
assertTerminated(phaser, maxPhase, 0); |
54 |
+ |
} |
55 |
+ |
|
56 |
|
/** |
57 |
|
* Empty constructor builds a new Phaser with no parent, no registered |
58 |
|
* parties and initial phase number of 0 |
59 |
|
*/ |
60 |
< |
public void testConstructor1() { |
60 |
> |
public void testConstructorDefaultValues() { |
61 |
|
Phaser phaser = new Phaser(); |
62 |
|
assertNull(phaser.getParent()); |
63 |
+ |
assertEquals(0, phaser.getRegisteredParties()); |
64 |
|
assertEquals(0, phaser.getArrivedParties()); |
65 |
+ |
assertEquals(0, phaser.getUnarrivedParties()); |
66 |
|
assertEquals(0, phaser.getPhase()); |
67 |
|
} |
68 |
|
|
69 |
|
/** |
70 |
< |
* A negative party number for the constructor throws illegal argument |
71 |
< |
* exception |
70 |
> |
* Constructing with a negative number of parties throws |
71 |
> |
* IllegalArgumentException |
72 |
|
*/ |
73 |
< |
public void testConstructor2() { |
73 |
> |
public void testConstructorNegativeParties() { |
74 |
|
try { |
75 |
|
new Phaser(-1); |
76 |
< |
this.shouldThrow(); |
77 |
< |
} catch (IllegalArgumentException success) { |
45 |
< |
} |
76 |
> |
shouldThrow(); |
77 |
> |
} catch (IllegalArgumentException success) {} |
78 |
|
} |
79 |
|
|
80 |
|
/** |
81 |
< |
* The parent being input into the constructor should equal the original |
82 |
< |
* parent when being returned |
81 |
> |
* Constructing with a negative number of parties throws |
82 |
> |
* IllegalArgumentException |
83 |
|
*/ |
84 |
< |
public void testConstructor3() { |
85 |
< |
Phaser parent = new Phaser(); |
86 |
< |
assertEquals(parent, new Phaser(parent).getParent()); |
84 |
> |
public void testConstructorNegativeParties2() { |
85 |
> |
try { |
86 |
> |
new Phaser(new Phaser(), -1); |
87 |
> |
shouldThrow(); |
88 |
> |
} catch (IllegalArgumentException success) {} |
89 |
|
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* A negative party number for the constructor throws illegal argument |
93 |
< |
* exception |
92 |
> |
* Constructing with a number of parties > 65535 throws |
93 |
> |
* IllegalArgumentException |
94 |
|
*/ |
95 |
< |
public void testConstructor4() { |
95 |
> |
public void testConstructorPartiesExceedsLimit() { |
96 |
> |
new Phaser(maxParties); |
97 |
|
try { |
98 |
< |
new Phaser(new Phaser(), -1); |
98 |
> |
new Phaser(maxParties + 1); |
99 |
|
shouldThrow(); |
100 |
< |
} catch (IllegalArgumentException success) { |
101 |
< |
} |
100 |
> |
} catch (IllegalArgumentException success) {} |
101 |
> |
|
102 |
> |
new Phaser(new Phaser(), maxParties); |
103 |
> |
try { |
104 |
> |
new Phaser(new Phaser(), maxParties + 1); |
105 |
> |
shouldThrow(); |
106 |
> |
} catch (IllegalArgumentException success) {} |
107 |
> |
} |
108 |
> |
|
109 |
> |
/** |
110 |
> |
* The parent provided to the constructor should be returned from |
111 |
> |
* a later call to getParent |
112 |
> |
*/ |
113 |
> |
public void testConstructor3() { |
114 |
> |
Phaser parent = new Phaser(); |
115 |
> |
assertSame(parent, new Phaser(parent).getParent()); |
116 |
> |
assertNull(new Phaser(null).getParent()); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
122 |
|
*/ |
123 |
|
public void testConstructor5() { |
124 |
|
Phaser parent = new Phaser(); |
125 |
< |
assertEquals(parent, new Phaser(parent, 0).getParent()); |
125 |
> |
assertSame(parent, new Phaser(parent, 0).getParent()); |
126 |
> |
assertNull(new Phaser(null, 0).getParent()); |
127 |
|
} |
128 |
|
|
129 |
|
/** |
130 |
< |
* register() will increment the number of unarrived parties by one and not |
131 |
< |
* affect its arrived parties |
130 |
> |
* register() will increment the number of unarrived parties by |
131 |
> |
* one and not affect its arrived parties |
132 |
|
*/ |
133 |
|
public void testRegister1() { |
134 |
|
Phaser phaser = new Phaser(); |
135 |
< |
assertEquals(0, phaser.getUnarrivedParties()); |
136 |
< |
phaser.register(); |
137 |
< |
assertEquals(1, phaser.getUnarrivedParties()); |
87 |
< |
assertEquals(0, phaser.getArrivedParties()); |
135 |
> |
assertState(phaser, 0, 0, 0); |
136 |
> |
assertEquals(0, phaser.register()); |
137 |
> |
assertState(phaser, 0, 1, 1); |
138 |
|
} |
139 |
|
|
140 |
|
/** |
141 |
< |
* Registering any more then 65536 parties causes IllegalStateExceptiom |
141 |
> |
* Registering more than 65536 parties causes IllegalStateException |
142 |
|
*/ |
143 |
|
public void testRegister2() { |
144 |
|
Phaser phaser = new Phaser(0); |
145 |
< |
int expectedUnnarivedParties = (1 << 16) - 1; |
146 |
< |
for (int i = 0; i < expectedUnnarivedParties; i++) { |
147 |
< |
phaser.register(); |
148 |
< |
assertEquals(i + 1, phaser.getUnarrivedParties()); |
145 |
> |
assertState(phaser, 0, 0, 0); |
146 |
> |
assertEquals(0, phaser.bulkRegister(maxParties - 10)); |
147 |
> |
assertState(phaser, 0, maxParties - 10, maxParties - 10); |
148 |
> |
for (int i = 0; i < 10; i++) { |
149 |
> |
assertState(phaser, 0, maxParties - 10 + i, maxParties - 10 + i); |
150 |
> |
assertEquals(0, phaser.register()); |
151 |
|
} |
152 |
+ |
assertState(phaser, 0, maxParties, maxParties); |
153 |
|
try { |
154 |
|
phaser.register(); |
155 |
|
shouldThrow(); |
156 |
< |
} catch (IllegalStateException success) { |
157 |
< |
} catch (Exception ex) { |
158 |
< |
threadUnexpectedException(ex); |
159 |
< |
} |
156 |
> |
} catch (IllegalStateException success) {} |
157 |
> |
|
158 |
> |
try { |
159 |
> |
phaser.bulkRegister(Integer.MAX_VALUE); |
160 |
> |
shouldThrow(); |
161 |
> |
} catch (IllegalStateException success) {} |
162 |
> |
|
163 |
> |
assertEquals(0, phaser.bulkRegister(0)); |
164 |
> |
assertState(phaser, 0, maxParties, maxParties); |
165 |
|
} |
166 |
|
|
167 |
|
/** |
171 |
|
public void testRegister3() { |
172 |
|
Phaser phaser = new Phaser(); |
173 |
|
assertEquals(0, phaser.register()); |
174 |
< |
phaser.arrive(); |
174 |
> |
assertEquals(0, phaser.arrive()); |
175 |
|
assertEquals(1, phaser.register()); |
176 |
+ |
assertState(phaser, 1, 2, 2); |
177 |
|
} |
178 |
|
|
179 |
|
/** |
182 |
|
*/ |
183 |
|
public void testRegister4() { |
184 |
|
Phaser phaser = new Phaser(1); |
185 |
< |
phaser.arrive(); |
186 |
< |
int expectedPhase = phaser.register(); |
187 |
< |
phaser.arrive(); |
188 |
< |
assertEquals(expectedPhase, phaser.getPhase()); |
130 |
< |
} |
131 |
< |
|
132 |
< |
public void testRegister5() { |
133 |
< |
Phaser phaser = new Phaser(); |
134 |
< |
phaser.register(); |
135 |
< |
assertEquals(1, phaser.getUnarrivedParties()); |
185 |
> |
assertEquals(0, phaser.arrive()); |
186 |
> |
assertEquals(1, phaser.register()); |
187 |
> |
assertEquals(1, phaser.arrive()); |
188 |
> |
assertState(phaser, 1, 2, 1); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
|
* Invoking bulkRegister with a negative parameter throws an |
193 |
< |
* IllegalArgumentExceptiom |
193 |
> |
* IllegalArgumentException |
194 |
|
*/ |
195 |
|
public void testBulkRegister1() { |
196 |
|
try { |
197 |
|
new Phaser().bulkRegister(-1); |
198 |
|
shouldThrow(); |
199 |
< |
} catch (IllegalArgumentException success) { |
147 |
< |
} |
199 |
> |
} catch (IllegalArgumentException success) {} |
200 |
|
} |
201 |
|
|
202 |
|
/** |
205 |
|
*/ |
206 |
|
public void testBulkRegister2() { |
207 |
|
Phaser phaser = new Phaser(); |
208 |
< |
phaser.bulkRegister(20); |
209 |
< |
assertEquals(20, phaser.getUnarrivedParties()); |
208 |
> |
assertEquals(0, phaser.bulkRegister(0)); |
209 |
> |
assertState(phaser, 0, 0, 0); |
210 |
> |
assertEquals(0, phaser.bulkRegister(20)); |
211 |
> |
assertState(phaser, 0, 20, 20); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
< |
* Registering with a number of parties greater then or equal to 1<<16 |
216 |
< |
* throws IllegalStateExceptiom. |
215 |
> |
* Registering with a number of parties greater than or equal to 1<<16 |
216 |
> |
* throws IllegalStateException. |
217 |
|
*/ |
218 |
|
public void testBulkRegister3() { |
219 |
+ |
assertEquals(0, new Phaser().bulkRegister((1 << 16) - 1)); |
220 |
+ |
|
221 |
|
try { |
222 |
|
new Phaser().bulkRegister(1 << 16); |
223 |
|
shouldThrow(); |
224 |
< |
} catch (IllegalStateException success) { |
225 |
< |
} |
224 |
> |
} catch (IllegalStateException success) {} |
225 |
> |
|
226 |
> |
try { |
227 |
> |
new Phaser(2).bulkRegister((1 << 16) - 2); |
228 |
> |
shouldThrow(); |
229 |
> |
} catch (IllegalStateException success) {} |
230 |
|
} |
231 |
|
|
232 |
|
/** |
243 |
|
} |
244 |
|
|
245 |
|
/** |
246 |
< |
* Arrive() on a registered phaser increments phase. |
246 |
> |
* arrive() on a registered phaser increments phase. |
247 |
|
*/ |
248 |
|
public void testArrive1() { |
249 |
|
Phaser phaser = new Phaser(1); |
250 |
< |
phaser.arrive(); |
251 |
< |
assertEquals(1, phaser.getPhase()); |
250 |
> |
assertState(phaser, 0, 1, 1); |
251 |
> |
assertEquals(0, phaser.arrive()); |
252 |
> |
assertState(phaser, 1, 1, 1); |
253 |
|
} |
254 |
|
|
255 |
|
/** |
256 |
< |
* arrive does not wait for others to arrive at barrier |
256 |
> |
* arriveAndDeregister does not wait for others to arrive at barrier |
257 |
|
*/ |
258 |
< |
public void testArrive2() { |
258 |
> |
public void testArriveAndDeregister() { |
259 |
|
final Phaser phaser = new Phaser(1); |
260 |
< |
phaser.register(); |
261 |
< |
Thread thread = null; |
262 |
< |
for (final Runnable r : getRunnables(10, SHORT_DELAY_MS)) { |
263 |
< |
phaser.register(); |
264 |
< |
thread = new Thread() { |
260 |
> |
for (int i = 0; i < 10; i++) { |
261 |
> |
assertState(phaser, 0, 1, 1); |
262 |
> |
assertEquals(0, phaser.register()); |
263 |
> |
assertState(phaser, 0, 2, 2); |
264 |
> |
assertEquals(0, phaser.arriveAndDeregister()); |
265 |
> |
assertState(phaser, 0, 1, 1); |
266 |
> |
} |
267 |
> |
assertEquals(0, phaser.arriveAndDeregister()); |
268 |
> |
assertTerminated(phaser, 1); |
269 |
> |
} |
270 |
|
|
271 |
< |
public void run() { |
272 |
< |
r.run(); |
273 |
< |
phaser.arriveAndDeregister(); |
274 |
< |
} |
275 |
< |
}; |
276 |
< |
thread.start(); |
271 |
> |
/** |
272 |
> |
* arriveAndDeregister does not wait for others to arrive at barrier |
273 |
> |
*/ |
274 |
> |
public void testArrive2() { |
275 |
> |
final Phaser phaser = new Phaser(); |
276 |
> |
assertEquals(0, phaser.register()); |
277 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
278 |
> |
for (int i = 0; i < 10; i++) { |
279 |
> |
assertEquals(0, phaser.register()); |
280 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
281 |
> |
public void realRun() { |
282 |
> |
assertEquals(0, phaser.arriveAndDeregister()); |
283 |
> |
}})); |
284 |
|
} |
285 |
|
|
286 |
< |
phaser.arrive(); |
287 |
< |
assertTrue(thread.isAlive()); |
288 |
< |
assertFalse(phaser.isTerminated()); |
286 |
> |
for (Thread thread : threads) |
287 |
> |
awaitTermination(thread); |
288 |
> |
assertState(phaser, 0, 1, 1); |
289 |
> |
assertEquals(0, phaser.arrive()); |
290 |
> |
assertState(phaser, 1, 1, 1); |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
< |
* arrive() returns a negative number if the Phaser is termindated |
294 |
> |
* arrive() returns a negative number if the Phaser is terminated |
295 |
|
*/ |
296 |
|
public void testArrive3() { |
297 |
|
Phaser phaser = new Phaser(1); |
298 |
|
phaser.forceTermination(); |
299 |
+ |
assertTerminated(phaser, 0, 1); |
300 |
+ |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
301 |
|
assertTrue(phaser.arrive() < 0); |
302 |
< |
|
302 |
> |
assertTrue(phaser.register() < 0); |
303 |
> |
assertTrue(phaser.arriveAndDeregister() < 0); |
304 |
> |
assertTrue(phaser.awaitAdvance(1) < 0); |
305 |
> |
assertTrue(phaser.getPhase() < 0); |
306 |
|
} |
307 |
|
|
308 |
|
/** |
309 |
|
* arriveAndDeregister() throws IllegalStateException if number of |
310 |
< |
* registered or unnarived parties would become negative |
310 |
> |
* registered or unarrived parties would become negative |
311 |
|
*/ |
312 |
|
public void testArriveAndDeregister1() { |
313 |
|
try { |
314 |
|
Phaser phaser = new Phaser(); |
315 |
|
phaser.arriveAndDeregister(); |
316 |
|
shouldThrow(); |
317 |
< |
|
238 |
< |
} catch (IllegalStateException success) { |
239 |
< |
} |
317 |
> |
} catch (IllegalStateException success) {} |
318 |
|
} |
319 |
|
|
320 |
|
/** |
321 |
< |
* arriveAndDeregister derigisters reduces the number of arrived parties |
321 |
> |
* arriveAndDeregister reduces the number of arrived parties |
322 |
|
*/ |
323 |
< |
public void testArriveAndDergeister2() { |
323 |
> |
public void testArriveAndDeregister2() { |
324 |
|
final Phaser phaser = new Phaser(1); |
325 |
< |
phaser.register(); |
326 |
< |
phaser.arrive(); |
327 |
< |
int p = phaser.getArrivedParties(); |
328 |
< |
assertTrue(p == 1); |
329 |
< |
phaser.arriveAndDeregister(); |
252 |
< |
assertTrue(phaser.getArrivedParties() < p); |
325 |
> |
assertEquals(0, phaser.register()); |
326 |
> |
assertEquals(0, phaser.arrive()); |
327 |
> |
assertState(phaser, 0, 2, 1); |
328 |
> |
assertEquals(0, phaser.arriveAndDeregister()); |
329 |
> |
assertState(phaser, 1, 1, 1); |
330 |
|
} |
331 |
|
|
332 |
|
/** |
333 |
< |
* arriveAndDeregister arrives to the barrier on a phaser with a parent and |
333 |
> |
* arriveAndDeregister arrives at the barrier on a phaser with a parent and |
334 |
|
* when a deregistration occurs and causes the phaser to have zero parties |
335 |
|
* its parent will be deregistered as well |
336 |
|
*/ |
337 |
< |
public void testArriveAndDeregsiter3() { |
337 |
> |
public void testArriveAndDeregister3() { |
338 |
|
Phaser parent = new Phaser(); |
339 |
< |
Phaser root = new Phaser(parent); |
340 |
< |
root.register(); |
341 |
< |
assertTrue(parent.getUnarrivedParties() > 0); |
342 |
< |
assertTrue(root.getUnarrivedParties() > 0); |
343 |
< |
root.arriveAndDeregister(); |
344 |
< |
assertTrue(parent.getUnarrivedParties() == 0); |
345 |
< |
assertTrue(root.getUnarrivedParties() == 0); |
346 |
< |
assertTrue(root.isTerminated() && parent.isTerminated()); |
339 |
> |
Phaser child = new Phaser(parent); |
340 |
> |
assertState(child, 0, 0, 0); |
341 |
> |
assertState(parent, 0, 0, 0); |
342 |
> |
assertEquals(0, child.register()); |
343 |
> |
assertState(child, 0, 1, 1); |
344 |
> |
assertState(parent, 0, 1, 1); |
345 |
> |
assertEquals(0, child.arriveAndDeregister()); |
346 |
> |
assertTerminated(child, 1); |
347 |
> |
assertTerminated(parent, 1); |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
|
* arriveAndDeregister deregisters one party from its parent when |
352 |
< |
* the number of parties of root is zero after deregistration |
352 |
> |
* the number of parties of child is zero after deregistration |
353 |
|
*/ |
354 |
< |
public void testArriveAndDeregsiter4() { |
354 |
> |
public void testArriveAndDeregister4() { |
355 |
|
Phaser parent = new Phaser(); |
356 |
< |
Phaser root = new Phaser(parent); |
357 |
< |
parent.register(); |
358 |
< |
root.register(); |
359 |
< |
int parentParties = parent.getUnarrivedParties(); |
360 |
< |
root.arriveAndDeregister(); |
361 |
< |
assertEquals(parentParties - 1, parent.getUnarrivedParties()); |
356 |
> |
Phaser child = new Phaser(parent); |
357 |
> |
assertEquals(0, parent.register()); |
358 |
> |
assertEquals(0, child.register()); |
359 |
> |
assertState(child, 0, 1, 1); |
360 |
> |
assertState(parent, 0, 2, 2); |
361 |
> |
assertEquals(0, child.arriveAndDeregister()); |
362 |
> |
assertState(child, 0, 0, 0); |
363 |
> |
assertState(parent, 0, 1, 1); |
364 |
|
} |
365 |
|
|
366 |
|
/** |
368 |
|
* the number of parties of root is nonzero after deregistration. |
369 |
|
*/ |
370 |
|
public void testArriveAndDeregister5() { |
371 |
< |
Phaser parent = new Phaser(); |
371 |
> |
Phaser root = new Phaser(); |
372 |
> |
Phaser parent = new Phaser(root); |
373 |
|
Phaser child = new Phaser(parent); |
374 |
< |
Phaser root = new Phaser(child); |
375 |
< |
assertTrue(parent.getUnarrivedParties() > 0); |
376 |
< |
assertTrue(child.getUnarrivedParties() > 0); |
377 |
< |
root.register(); |
378 |
< |
root.arriveAndDeregister(); |
379 |
< |
assertTrue(parent.getUnarrivedParties() == 0); |
380 |
< |
assertTrue(child.getUnarrivedParties() == 0); |
381 |
< |
assertTrue(root.isTerminated()); |
374 |
> |
assertState(root, 0, 0, 0); |
375 |
> |
assertState(parent, 0, 0, 0); |
376 |
> |
assertState(child, 0, 0, 0); |
377 |
> |
assertEquals(0, child.register()); |
378 |
> |
assertState(root, 0, 1, 1); |
379 |
> |
assertState(parent, 0, 1, 1); |
380 |
> |
assertState(child, 0, 1, 1); |
381 |
> |
assertEquals(0, child.arriveAndDeregister()); |
382 |
> |
assertTerminated(child, 1); |
383 |
> |
assertTerminated(parent, 1); |
384 |
> |
assertTerminated(root, 1); |
385 |
|
} |
386 |
|
|
387 |
|
/** |
390 |
|
*/ |
391 |
|
public void testArriveAndDeregister6() { |
392 |
|
final Phaser phaser = new Phaser(2); |
393 |
< |
new Thread() { |
394 |
< |
|
395 |
< |
public void run() { |
396 |
< |
getRunnable(SHORT_DELAY_MS).run(); |
397 |
< |
phaser.arrive(); |
398 |
< |
} |
399 |
< |
}.start(); |
400 |
< |
phaser.arriveAndAwaitAdvance(); |
401 |
< |
int phase = phaser.arriveAndDeregister(); |
402 |
< |
assertEquals(phase, phaser.getPhase()); |
393 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
394 |
> |
public void realRun() { |
395 |
> |
assertEquals(0, phaser.arrive()); |
396 |
> |
}}); |
397 |
> |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
398 |
> |
assertState(phaser, 1, 2, 2); |
399 |
> |
assertEquals(1, phaser.arriveAndDeregister()); |
400 |
> |
assertState(phaser, 1, 1, 1); |
401 |
> |
assertEquals(1, phaser.arriveAndDeregister()); |
402 |
> |
assertTerminated(phaser, 2); |
403 |
> |
awaitTermination(t); |
404 |
|
} |
405 |
|
|
406 |
|
/** |
408 |
|
*/ |
409 |
|
public void testAwaitAdvance1() { |
410 |
|
final Phaser phaser = new Phaser(1); |
411 |
< |
phaser.awaitAdvance(phaser.arrive()); |
411 |
> |
assertEquals(0, phaser.arrive()); |
412 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
413 |
|
} |
414 |
|
|
415 |
|
/** |
417 |
|
* phaser |
418 |
|
*/ |
419 |
|
public void testAwaitAdvance2() { |
420 |
< |
try { |
421 |
< |
Phaser phaser = new Phaser(); |
422 |
< |
phaser.awaitAdvance(-1); |
337 |
< |
} catch (Exception failure) { |
338 |
< |
this.unexpectedException(); |
339 |
< |
} |
420 |
> |
Phaser phaser = new Phaser(); |
421 |
> |
assertTrue(phaser.awaitAdvance(-1) < 0); |
422 |
> |
assertState(phaser, 0, 0, 0); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
< |
* awaitAdvance while waiting does not abort on interrupt. |
426 |
> |
* awaitAdvanceInterruptibly blocks interruptibly |
427 |
|
*/ |
428 |
< |
public void testAwaitAdvance3() { |
429 |
< |
final Phaser phaser = new Phaser(); |
430 |
< |
Thread th1 = new Thread() { |
428 |
> |
public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException { |
429 |
> |
final Phaser phaser = new Phaser(1); |
430 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
431 |
|
|
432 |
< |
public void run() { |
432 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
433 |
> |
public void realRun() { |
434 |
> |
Thread.currentThread().interrupt(); |
435 |
|
try { |
436 |
< |
phaser.register(); |
437 |
< |
getRunnable(LONG_DELAY_MS).run(); |
438 |
< |
phaser.awaitAdvance(phaser.arrive()); |
439 |
< |
} catch (Exception failure) { |
355 |
< |
threadUnexpectedException(failure); |
356 |
< |
} |
436 |
> |
phaser.awaitAdvanceInterruptibly(0); |
437 |
> |
shouldThrow(); |
438 |
> |
} catch (InterruptedException success) {} |
439 |
> |
assertFalse(Thread.interrupted()); |
440 |
|
|
441 |
< |
} |
442 |
< |
}; |
443 |
< |
phaser.register(); |
444 |
< |
th1.start(); |
445 |
< |
try { |
446 |
< |
Thread.sleep(SHORT_DELAY_MS); |
447 |
< |
th1.interrupt(); |
448 |
< |
Thread.sleep(LONG_DELAY_MS); |
449 |
< |
phaser.arrive(); |
450 |
< |
} catch (Exception failure) { |
451 |
< |
unexpectedException(); |
452 |
< |
} |
453 |
< |
assertFalse(th1.isInterrupted()); |
441 |
> |
pleaseInterrupt.countDown(); |
442 |
> |
try { |
443 |
> |
phaser.awaitAdvanceInterruptibly(0); |
444 |
> |
shouldThrow(); |
445 |
> |
} catch (InterruptedException success) {} |
446 |
> |
assertFalse(Thread.interrupted()); |
447 |
> |
}}); |
448 |
> |
|
449 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
450 |
> |
public void realRun() throws TimeoutException { |
451 |
> |
Thread.currentThread().interrupt(); |
452 |
> |
try { |
453 |
> |
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); |
454 |
> |
shouldThrow(); |
455 |
> |
} catch (InterruptedException success) {} |
456 |
> |
assertFalse(Thread.interrupted()); |
457 |
> |
|
458 |
> |
pleaseInterrupt.countDown(); |
459 |
> |
try { |
460 |
> |
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); |
461 |
> |
shouldThrow(); |
462 |
> |
} catch (InterruptedException success) {} |
463 |
> |
assertFalse(Thread.interrupted()); |
464 |
> |
}}); |
465 |
> |
|
466 |
> |
await(pleaseInterrupt); |
467 |
> |
assertState(phaser, 0, 1, 1); |
468 |
> |
assertThreadsStayAlive(t1, t2); |
469 |
> |
t1.interrupt(); |
470 |
> |
t2.interrupt(); |
471 |
> |
awaitTermination(t1); |
472 |
> |
awaitTermination(t2); |
473 |
> |
assertState(phaser, 0, 1, 1); |
474 |
> |
assertEquals(0, phaser.arrive()); |
475 |
> |
assertState(phaser, 1, 1, 1); |
476 |
> |
} |
477 |
> |
|
478 |
> |
/** |
479 |
> |
* awaitAdvance continues waiting if interrupted before waiting |
480 |
> |
*/ |
481 |
> |
public void testAwaitAdvanceAfterInterrupt() { |
482 |
> |
final Phaser phaser = new Phaser(); |
483 |
> |
assertEquals(0, phaser.register()); |
484 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(1); |
485 |
> |
|
486 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
487 |
> |
public void realRun() { |
488 |
> |
Thread.currentThread().interrupt(); |
489 |
> |
assertEquals(0, phaser.register()); |
490 |
> |
assertEquals(0, phaser.arrive()); |
491 |
> |
pleaseArrive.countDown(); |
492 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
493 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
494 |
> |
assertTrue(Thread.interrupted()); |
495 |
> |
}}); |
496 |
> |
|
497 |
> |
await(pleaseArrive); |
498 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
499 |
> |
assertEquals(0, phaser.arrive()); |
500 |
> |
awaitTermination(t); |
501 |
> |
|
502 |
> |
Thread.currentThread().interrupt(); |
503 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
504 |
> |
assertTrue(Thread.interrupted()); |
505 |
> |
} |
506 |
> |
|
507 |
> |
/** |
508 |
> |
* awaitAdvance continues waiting if interrupted while waiting |
509 |
> |
*/ |
510 |
> |
public void testAwaitAdvanceBeforeInterrupt() { |
511 |
> |
final Phaser phaser = new Phaser(); |
512 |
> |
assertEquals(0, phaser.register()); |
513 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(1); |
514 |
> |
|
515 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
516 |
> |
public void realRun() { |
517 |
> |
assertEquals(0, phaser.register()); |
518 |
> |
assertEquals(0, phaser.arrive()); |
519 |
> |
assertFalse(Thread.currentThread().isInterrupted()); |
520 |
> |
pleaseArrive.countDown(); |
521 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
522 |
> |
assertTrue(Thread.interrupted()); |
523 |
> |
}}); |
524 |
> |
|
525 |
> |
await(pleaseArrive); |
526 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
527 |
> |
t.interrupt(); |
528 |
> |
assertEquals(0, phaser.arrive()); |
529 |
> |
awaitTermination(t); |
530 |
> |
|
531 |
> |
Thread.currentThread().interrupt(); |
532 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
533 |
> |
assertTrue(Thread.interrupted()); |
534 |
> |
} |
535 |
> |
|
536 |
> |
/** |
537 |
> |
* arriveAndAwaitAdvance continues waiting if interrupted before waiting |
538 |
> |
*/ |
539 |
> |
public void testArriveAndAwaitAdvanceAfterInterrupt() { |
540 |
> |
final Phaser phaser = new Phaser(); |
541 |
> |
assertEquals(0, phaser.register()); |
542 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
543 |
> |
|
544 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
545 |
> |
public void realRun() { |
546 |
> |
Thread.currentThread().interrupt(); |
547 |
> |
assertEquals(0, phaser.register()); |
548 |
> |
pleaseInterrupt.countDown(); |
549 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
550 |
> |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
551 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
552 |
> |
}}); |
553 |
> |
|
554 |
> |
await(pleaseInterrupt); |
555 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
556 |
> |
Thread.currentThread().interrupt(); |
557 |
> |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
558 |
> |
assertTrue(Thread.interrupted()); |
559 |
> |
awaitTermination(t); |
560 |
> |
} |
561 |
> |
|
562 |
> |
/** |
563 |
> |
* arriveAndAwaitAdvance continues waiting if interrupted while waiting |
564 |
> |
*/ |
565 |
> |
public void testArriveAndAwaitAdvanceBeforeInterrupt() { |
566 |
> |
final Phaser phaser = new Phaser(); |
567 |
> |
assertEquals(0, phaser.register()); |
568 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
569 |
> |
|
570 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
571 |
> |
public void realRun() { |
572 |
> |
assertEquals(0, phaser.register()); |
573 |
> |
assertFalse(Thread.currentThread().isInterrupted()); |
574 |
> |
pleaseInterrupt.countDown(); |
575 |
> |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
576 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
577 |
> |
}}); |
578 |
> |
|
579 |
> |
await(pleaseInterrupt); |
580 |
> |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
581 |
> |
t.interrupt(); |
582 |
> |
Thread.currentThread().interrupt(); |
583 |
> |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
584 |
> |
assertTrue(Thread.interrupted()); |
585 |
> |
awaitTermination(t); |
586 |
|
} |
587 |
|
|
588 |
|
/** |
590 |
|
* complete before continuing |
591 |
|
*/ |
592 |
|
public void testAwaitAdvance4() { |
593 |
< |
final Phaser phaser = new Phaser(four); |
594 |
< |
final AtomicInteger phaseCount = new AtomicInteger(0); |
595 |
< |
for (int i = 0; i < four; i++) { |
596 |
< |
new Thread() { |
597 |
< |
|
598 |
< |
public void run() { |
599 |
< |
int phase = phaser.arrive(); |
600 |
< |
phaseCount.incrementAndGet(); |
601 |
< |
getRunnable(LONG_DELAY_MS).run(); |
602 |
< |
phaser.awaitAdvance(phase); |
603 |
< |
assertTrue(phaseCount.get() == four); |
604 |
< |
} |
605 |
< |
}.start(); |
606 |
< |
} |
593 |
> |
final Phaser phaser = new Phaser(4); |
594 |
> |
final AtomicInteger count = new AtomicInteger(0); |
595 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
596 |
> |
for (int i = 0; i < 4; i++) |
597 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
598 |
> |
public void realRun() { |
599 |
> |
for (int k = 0; k < 3; k++) { |
600 |
> |
assertEquals(2*k+1, phaser.arriveAndAwaitAdvance()); |
601 |
> |
count.incrementAndGet(); |
602 |
> |
assertEquals(2*k+1, phaser.arrive()); |
603 |
> |
assertEquals(2*k+2, phaser.awaitAdvance(2*k+1)); |
604 |
> |
assertEquals(count.get(), 4*(k+1)); |
605 |
> |
}}})); |
606 |
> |
|
607 |
> |
for (Thread thread : threads) |
608 |
> |
awaitTermination(thread); |
609 |
|
} |
610 |
|
|
611 |
|
/** |
613 |
|
*/ |
614 |
|
public void testAwaitAdvance5() { |
615 |
|
final Phaser phaser = new Phaser(1); |
616 |
< |
int phase = phaser.awaitAdvance(phaser.arrive()); |
617 |
< |
assertEquals(phase, phaser.getPhase()); |
618 |
< |
phaser.register(); |
619 |
< |
for (int i = 0; i < eight; i++) { |
620 |
< |
new Thread() { |
621 |
< |
|
622 |
< |
public void run() { |
623 |
< |
getRunnable(SHORT_DELAY_MS).run(); |
616 |
> |
assertEquals(1, phaser.awaitAdvance(phaser.arrive())); |
617 |
> |
assertEquals(1, phaser.getPhase()); |
618 |
> |
assertEquals(1, phaser.register()); |
619 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
620 |
> |
for (int i = 0; i < 8; i++) { |
621 |
> |
final CountDownLatch latch = new CountDownLatch(1); |
622 |
> |
final boolean goesFirst = ((i & 1) == 0); |
623 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
624 |
> |
public void realRun() { |
625 |
> |
if (goesFirst) |
626 |
> |
latch.countDown(); |
627 |
> |
else |
628 |
> |
await(latch); |
629 |
|
phaser.arrive(); |
630 |
< |
} |
631 |
< |
}.start(); |
632 |
< |
phase = phaser.awaitAdvance(phaser.arrive()); |
633 |
< |
assertEquals(phase, phaser.getPhase()); |
630 |
> |
}})); |
631 |
> |
if (goesFirst) |
632 |
> |
await(latch); |
633 |
> |
else |
634 |
> |
latch.countDown(); |
635 |
> |
assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive())); |
636 |
> |
assertEquals(i + 2, phaser.getPhase()); |
637 |
> |
} |
638 |
> |
for (Thread thread : threads) |
639 |
> |
awaitTermination(thread); |
640 |
> |
} |
641 |
> |
|
642 |
> |
/** |
643 |
> |
* awaitAdvance returns the current phase in child phasers |
644 |
> |
*/ |
645 |
> |
public void testAwaitAdvanceTieredPhaser() throws Exception { |
646 |
> |
final Phaser parent = new Phaser(); |
647 |
> |
final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3); |
648 |
> |
final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3); |
649 |
> |
for (int i = 0; i < 3; i++) { |
650 |
> |
zeroPartyChildren.add(new Phaser(parent, 0)); |
651 |
> |
onePartyChildren.add(new Phaser(parent, 1)); |
652 |
> |
} |
653 |
> |
final List<Phaser> phasers = new ArrayList<Phaser>(); |
654 |
> |
phasers.addAll(zeroPartyChildren); |
655 |
> |
phasers.addAll(onePartyChildren); |
656 |
> |
phasers.add(parent); |
657 |
> |
for (Phaser phaser : phasers) { |
658 |
> |
assertEquals(-42, phaser.awaitAdvance(-42)); |
659 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
660 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
661 |
> |
} |
662 |
> |
|
663 |
> |
for (Phaser child : onePartyChildren) |
664 |
> |
assertEquals(0, child.arrive()); |
665 |
> |
for (Phaser phaser : phasers) { |
666 |
> |
assertEquals(-42, phaser.awaitAdvance(-42)); |
667 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
668 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
669 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
670 |
> |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0)); |
671 |
> |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
672 |
> |
} |
673 |
> |
|
674 |
> |
for (Phaser child : onePartyChildren) |
675 |
> |
assertEquals(1, child.arrive()); |
676 |
> |
for (Phaser phaser : phasers) { |
677 |
> |
assertEquals(-42, phaser.awaitAdvance(-42)); |
678 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
679 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
680 |
> |
assertEquals(2, phaser.awaitAdvance(0)); |
681 |
> |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0)); |
682 |
> |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
683 |
> |
assertEquals(2, phaser.awaitAdvance(1)); |
684 |
> |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1)); |
685 |
> |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS)); |
686 |
|
} |
687 |
|
} |
688 |
|
|
691 |
|
*/ |
692 |
|
public void testAwaitAdvance6() { |
693 |
|
final Phaser phaser = new Phaser(3); |
694 |
< |
/* |
695 |
< |
* Start new thread. This thread waits a small amount of time |
696 |
< |
* and waits for the other two parties to arrive. The party |
697 |
< |
* in the main thread arrives quickly so at best this thread |
698 |
< |
* waits for the second thread's party to arrive |
699 |
< |
*/ |
700 |
< |
new Thread() { |
701 |
< |
|
702 |
< |
public void run() { |
703 |
< |
getRunnable(SMALL_DELAY_MS).run(); |
704 |
< |
int phase = phaser.awaitAdvance(phaser.arrive()); |
705 |
< |
/* |
706 |
< |
* This point is reached when force termination is called in which phase = -1 |
707 |
< |
*/ |
708 |
< |
threadAssertTrue(phase < 0); |
709 |
< |
threadAssertTrue(phaser.isTerminated()); |
436 |
< |
} |
437 |
< |
}.start(); |
438 |
< |
/* |
439 |
< |
* This thread will cause the first thread run to wait, in doing so |
440 |
< |
* the main thread will force termination in which the first thread |
441 |
< |
* should exit peacefully as this one |
442 |
< |
*/ |
443 |
< |
new Thread() { |
444 |
< |
|
445 |
< |
public void run() { |
446 |
< |
getRunnable(LONG_DELAY_MS).run(); |
447 |
< |
int p1 = phaser.arrive(); |
448 |
< |
int phase = phaser.awaitAdvance(p1); |
449 |
< |
threadAssertTrue(phase < 0); |
450 |
< |
threadAssertTrue(phaser.isTerminated()); |
451 |
< |
} |
452 |
< |
}.start(); |
453 |
< |
|
454 |
< |
phaser.arrive(); |
694 |
> |
final CountDownLatch pleaseForceTermination = new CountDownLatch(2); |
695 |
> |
final List<Thread> threads = new ArrayList<Thread>(); |
696 |
> |
for (int i = 0; i < 2; i++) { |
697 |
> |
Runnable r = new CheckedRunnable() { |
698 |
> |
public void realRun() { |
699 |
> |
assertEquals(0, phaser.arrive()); |
700 |
> |
pleaseForceTermination.countDown(); |
701 |
> |
assertTrue(phaser.awaitAdvance(0) < 0); |
702 |
> |
assertTrue(phaser.isTerminated()); |
703 |
> |
assertTrue(phaser.getPhase() < 0); |
704 |
> |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
705 |
> |
assertEquals(3, phaser.getRegisteredParties()); |
706 |
> |
}}; |
707 |
> |
threads.add(newStartedThread(r)); |
708 |
> |
} |
709 |
> |
await(pleaseForceTermination); |
710 |
|
phaser.forceTermination(); |
711 |
+ |
assertTrue(phaser.isTerminated()); |
712 |
+ |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
713 |
+ |
for (Thread thread : threads) |
714 |
+ |
awaitTermination(thread); |
715 |
+ |
assertEquals(3, phaser.getRegisteredParties()); |
716 |
|
} |
717 |
|
|
718 |
|
/** |
724 |
|
Phaser phaser = new Phaser(); |
725 |
|
phaser.arriveAndAwaitAdvance(); |
726 |
|
shouldThrow(); |
727 |
< |
} catch (IllegalStateException success) { |
468 |
< |
} |
469 |
< |
} |
470 |
< |
|
471 |
< |
/** |
472 |
< |
* Interrupted arriveAndAwaitAdvance does not throw InterruptedException |
473 |
< |
*/ |
474 |
< |
public void testArriveAndAwaitAdvance2() { |
475 |
< |
final Phaser phaser = new Phaser(2); |
476 |
< |
Thread th = new Thread() { |
477 |
< |
public void run() { |
478 |
< |
try { |
479 |
< |
phaser.arriveAndAwaitAdvance(); |
480 |
< |
} catch (Exception failure) { |
481 |
< |
threadUnexpectedException(failure); |
482 |
< |
} |
483 |
< |
} |
484 |
< |
}; |
485 |
< |
|
486 |
< |
try { |
487 |
< |
th.start(); |
488 |
< |
Thread.sleep(LONG_DELAY_MS); |
489 |
< |
th.interrupt(); |
490 |
< |
Thread.sleep(LONG_DELAY_MS); |
491 |
< |
phaser.arrive(); |
492 |
< |
} catch (InterruptedException failure) { |
493 |
< |
this.unexpectedException(); |
494 |
< |
} |
495 |
< |
assertFalse(th.isInterrupted()); |
727 |
> |
} catch (IllegalStateException success) {} |
728 |
|
} |
729 |
|
|
730 |
|
/** |
734 |
|
*/ |
735 |
|
public void testArriveAndAwaitAdvance3() { |
736 |
|
final Phaser phaser = new Phaser(1); |
737 |
< |
final AtomicInteger arrivingCount = new AtomicInteger(0); |
738 |
< |
for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) { |
739 |
< |
new Thread() { |
740 |
< |
|
741 |
< |
public void run() { |
742 |
< |
phaser.register(); |
743 |
< |
run.run(); |
744 |
< |
arrivingCount.getAndIncrement(); |
745 |
< |
phaser.arrive(); |
746 |
< |
} |
747 |
< |
}.start(); |
748 |
< |
} |
749 |
< |
int phaseNumber = phaser.arriveAndAwaitAdvance(); |
750 |
< |
arrivingCount.incrementAndGet(); |
751 |
< |
//the + 1 adds to expectedArrive to account for the main threads arrival |
752 |
< |
int expectedArrived = phaseNumber > 0 ? phaseNumber * six + 1 : phaser.getArrivedParties() + 1; |
753 |
< |
threadAssertEquals(expectedArrived, arrivingCount.get()); |
754 |
< |
} |
755 |
< |
// .. initially called, for n tasks via |
756 |
< |
private List<Runnable> getRunnables(int size, long wait) { |
757 |
< |
List<Runnable> list = new ArrayList<Runnable>(); |
758 |
< |
for (int i = 0; i < size; i++) { |
759 |
< |
list.add(getRunnable(wait)); |
760 |
< |
} |
761 |
< |
return list; |
762 |
< |
} |
531 |
< |
|
532 |
< |
private Runnable getRunnable(final long wait) { |
533 |
< |
return new Runnable() { |
534 |
< |
|
535 |
< |
public void run() { |
536 |
< |
try { |
537 |
< |
Thread.sleep(wait); |
538 |
< |
} catch (InterruptedException noop) { |
539 |
< |
// sleep interruption isn't a problem case for these example |
540 |
< |
} catch (Exception ex) { |
541 |
< |
threadUnexpectedException(ex); |
542 |
< |
} |
543 |
< |
|
544 |
< |
} |
545 |
< |
}; |
737 |
> |
final int THREADS = 3; |
738 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(THREADS); |
739 |
> |
final List<Thread> threads = new ArrayList<Thread>(); |
740 |
> |
for (int i = 0; i < THREADS; i++) |
741 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
742 |
> |
public void realRun() { |
743 |
> |
assertEquals(0, phaser.register()); |
744 |
> |
pleaseArrive.countDown(); |
745 |
> |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
746 |
> |
}})); |
747 |
> |
|
748 |
> |
await(pleaseArrive); |
749 |
> |
long startTime = System.nanoTime(); |
750 |
> |
while (phaser.getArrivedParties() < THREADS) |
751 |
> |
Thread.yield(); |
752 |
> |
assertEquals(THREADS, phaser.getArrivedParties()); |
753 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
754 |
> |
for (Thread thread : threads) |
755 |
> |
waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS); |
756 |
> |
for (Thread thread : threads) |
757 |
> |
assertTrue(thread.isAlive()); |
758 |
> |
assertState(phaser, 0, THREADS + 1, 1); |
759 |
> |
phaser.arriveAndAwaitAdvance(); |
760 |
> |
for (Thread thread : threads) |
761 |
> |
awaitTermination(thread); |
762 |
> |
assertState(phaser, 1, THREADS + 1, THREADS + 1); |
763 |
|
} |
764 |
|
|
765 |
|
} |