40 |
|
public void testConstructor2() { |
41 |
|
try { |
42 |
|
new Phaser(-1); |
43 |
< |
this.shouldThrow(); |
44 |
< |
} catch (IllegalArgumentException success) { |
45 |
< |
} |
43 |
> |
shouldThrow(); |
44 |
> |
} catch (IllegalArgumentException success) {} |
45 |
|
} |
46 |
|
|
47 |
|
/** |
61 |
|
try { |
62 |
|
new Phaser(new Phaser(), -1); |
63 |
|
shouldThrow(); |
64 |
< |
} catch (IllegalArgumentException success) { |
66 |
< |
} |
64 |
> |
} catch (IllegalArgumentException success) {} |
65 |
|
} |
66 |
|
|
67 |
|
/** |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* Registering any more then 65536 parties causes IllegalStateExceptiom |
89 |
> |
* Registering more than 65536 parties causes IllegalStateException |
90 |
|
*/ |
91 |
|
public void testRegister2() { |
92 |
|
Phaser phaser = new Phaser(0); |
98 |
|
try { |
99 |
|
phaser.register(); |
100 |
|
shouldThrow(); |
101 |
< |
} catch (IllegalStateException success) { |
104 |
< |
} catch (Exception ex) { |
105 |
< |
threadUnexpectedException(ex); |
106 |
< |
} |
101 |
> |
} catch (IllegalStateException success) {} |
102 |
|
} |
103 |
|
|
104 |
|
/** |
132 |
|
|
133 |
|
/** |
134 |
|
* Invoking bulkRegister with a negative parameter throws an |
135 |
< |
* IllegalArgumentExceptiom |
135 |
> |
* IllegalArgumentException |
136 |
|
*/ |
137 |
|
public void testBulkRegister1() { |
138 |
|
try { |
139 |
|
new Phaser().bulkRegister(-1); |
140 |
|
shouldThrow(); |
141 |
< |
} catch (IllegalArgumentException success) { |
147 |
< |
} |
141 |
> |
} catch (IllegalArgumentException success) {} |
142 |
|
} |
143 |
|
|
144 |
|
/** |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
< |
* Registering with a number of parties greater then or equal to 1<<16 |
156 |
< |
* throws IllegalStateExceptiom. |
155 |
> |
* Registering with a number of parties greater than or equal to 1<<16 |
156 |
> |
* throws IllegalStateException. |
157 |
|
*/ |
158 |
|
public void testBulkRegister3() { |
159 |
|
try { |
160 |
|
new Phaser().bulkRegister(1 << 16); |
161 |
|
shouldThrow(); |
162 |
< |
} catch (IllegalStateException success) { |
169 |
< |
} |
162 |
> |
} catch (IllegalStateException success) {} |
163 |
|
} |
164 |
|
|
165 |
|
/** |
185 |
|
} |
186 |
|
|
187 |
|
/** |
188 |
< |
* arrive does not wait for others to arrive at barrier |
188 |
> |
* arriveAndDeregister does not wait for others to arrive at barrier |
189 |
|
*/ |
190 |
< |
public void testArrive2() { |
190 |
> |
public void testArrive2() throws InterruptedException { |
191 |
|
final Phaser phaser = new Phaser(1); |
192 |
|
phaser.register(); |
193 |
< |
Thread thread = null; |
194 |
< |
for (final Runnable r : getRunnables(10, SHORT_DELAY_MS)) { |
193 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
194 |
> |
for (int i = 0; i < 10; i++) |
195 |
|
phaser.register(); |
196 |
< |
thread = new Thread() { |
197 |
< |
|
198 |
< |
public void run() { |
206 |
< |
r.run(); |
196 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
197 |
> |
public void realRun() throws InterruptedException { |
198 |
> |
Thread.sleep(SMALL_DELAY_MS); |
199 |
|
phaser.arriveAndDeregister(); |
200 |
< |
} |
209 |
< |
}; |
210 |
< |
thread.start(); |
211 |
< |
} |
200 |
> |
}})); |
201 |
|
|
202 |
|
phaser.arrive(); |
203 |
< |
assertTrue(thread.isAlive()); |
203 |
> |
assertTrue(threads.get(0).isAlive()); |
204 |
|
assertFalse(phaser.isTerminated()); |
205 |
+ |
for (Thread thread : threads) |
206 |
+ |
thread.join(); |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
< |
* arrive() returns a negative number if the Phaser is termindated |
210 |
> |
* arrive() returns a negative number if the Phaser is terminated |
211 |
|
*/ |
212 |
|
public void testArrive3() { |
213 |
|
Phaser phaser = new Phaser(1); |
214 |
|
phaser.forceTermination(); |
215 |
|
assertTrue(phaser.arrive() < 0); |
225 |
– |
|
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
|
* arriveAndDeregister() throws IllegalStateException if number of |
220 |
< |
* registered or unnarived parties would become negative |
220 |
> |
* registered or unarrived parties would become negative |
221 |
|
*/ |
222 |
|
public void testArriveAndDeregister1() { |
223 |
|
try { |
224 |
|
Phaser phaser = new Phaser(); |
225 |
|
phaser.arriveAndDeregister(); |
226 |
|
shouldThrow(); |
227 |
< |
|
238 |
< |
} catch (IllegalStateException success) { |
239 |
< |
} |
227 |
> |
} catch (IllegalStateException success) {} |
228 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
< |
* arriveAndDeregister derigisters reduces the number of arrived parties |
231 |
> |
* arriveAndDeregister deregisters reduces the number of arrived parties |
232 |
|
*/ |
233 |
|
public void testArriveAndDergeister2() { |
234 |
|
final Phaser phaser = new Phaser(1); |
292 |
|
* arriveAndDeregister returns the phase in which it leaves the |
293 |
|
* phaser in after deregistration |
294 |
|
*/ |
295 |
< |
public void testArriveAndDeregister6() { |
295 |
> |
public void testArriveAndDeregister6() throws InterruptedException { |
296 |
|
final Phaser phaser = new Phaser(2); |
297 |
< |
new Thread() { |
298 |
< |
|
299 |
< |
public void run() { |
312 |
< |
getRunnable(SHORT_DELAY_MS).run(); |
297 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
298 |
> |
public void realRun() { |
299 |
> |
sleepTillInterrupted(SHORT_DELAY_MS); |
300 |
|
phaser.arrive(); |
301 |
< |
} |
315 |
< |
}.start(); |
301 |
> |
}}); |
302 |
|
phaser.arriveAndAwaitAdvance(); |
303 |
|
int phase = phaser.arriveAndDeregister(); |
304 |
|
assertEquals(phase, phaser.getPhase()); |
305 |
+ |
t.join(); |
306 |
|
} |
307 |
|
|
308 |
|
/** |
318 |
|
* phaser |
319 |
|
*/ |
320 |
|
public void testAwaitAdvance2() { |
321 |
< |
try { |
322 |
< |
Phaser phaser = new Phaser(); |
336 |
< |
phaser.awaitAdvance(-1); |
337 |
< |
} catch (Exception failure) { |
338 |
< |
this.unexpectedException(); |
339 |
< |
} |
321 |
> |
Phaser phaser = new Phaser(); |
322 |
> |
phaser.awaitAdvance(-1); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
|
* awaitAdvance while waiting does not abort on interrupt. |
327 |
|
*/ |
328 |
< |
public void testAwaitAdvance3() { |
328 |
> |
public void testAwaitAdvance3() throws InterruptedException { |
329 |
|
final Phaser phaser = new Phaser(); |
347 |
– |
Thread th1 = new Thread() { |
348 |
– |
|
349 |
– |
public void run() { |
350 |
– |
try { |
351 |
– |
phaser.register(); |
352 |
– |
getRunnable(LONG_DELAY_MS).run(); |
353 |
– |
phaser.awaitAdvance(phaser.arrive()); |
354 |
– |
} catch (Exception failure) { |
355 |
– |
threadUnexpectedException(failure); |
356 |
– |
} |
357 |
– |
|
358 |
– |
} |
359 |
– |
}; |
330 |
|
phaser.register(); |
331 |
< |
th1.start(); |
332 |
< |
try { |
333 |
< |
Thread.sleep(SHORT_DELAY_MS); |
334 |
< |
th1.interrupt(); |
335 |
< |
Thread.sleep(LONG_DELAY_MS); |
336 |
< |
phaser.arrive(); |
337 |
< |
} catch (Exception failure) { |
338 |
< |
unexpectedException(); |
339 |
< |
} |
340 |
< |
assertFalse(th1.isInterrupted()); |
331 |
> |
|
332 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
333 |
> |
public void realRun() throws InterruptedException { |
334 |
> |
phaser.register(); |
335 |
> |
sleepTillInterrupted(LONG_DELAY_MS); |
336 |
> |
phaser.awaitAdvance(phaser.arrive()); |
337 |
> |
}}); |
338 |
> |
Thread.sleep(SMALL_DELAY_MS); |
339 |
> |
t.interrupt(); |
340 |
> |
Thread.sleep(SMALL_DELAY_MS); |
341 |
> |
phaser.arrive(); |
342 |
> |
assertFalse(t.isInterrupted()); |
343 |
> |
t.join(); |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
|
* awaitAdvance atomically waits for all parties within the same phase to |
348 |
|
* complete before continuing |
349 |
|
*/ |
350 |
< |
public void testAwaitAdvance4() { |
351 |
< |
final Phaser phaser = new Phaser(four); |
350 |
> |
public void testAwaitAdvance4() throws InterruptedException { |
351 |
> |
final Phaser phaser = new Phaser(4); |
352 |
|
final AtomicInteger phaseCount = new AtomicInteger(0); |
353 |
< |
for (int i = 0; i < four; i++) { |
354 |
< |
new Thread() { |
355 |
< |
|
356 |
< |
public void run() { |
353 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
354 |
> |
for (int i = 0; i < 4; i++) { |
355 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
356 |
> |
public void realRun() { |
357 |
|
int phase = phaser.arrive(); |
358 |
|
phaseCount.incrementAndGet(); |
359 |
< |
getRunnable(LONG_DELAY_MS).run(); |
359 |
> |
sleepTillInterrupted(SMALL_DELAY_MS); |
360 |
|
phaser.awaitAdvance(phase); |
361 |
< |
assertTrue(phaseCount.get() == four); |
362 |
< |
} |
390 |
< |
}.start(); |
361 |
> |
assertEquals(phaseCount.get(), 4); |
362 |
> |
}})); |
363 |
|
} |
364 |
+ |
for (Thread thread : threads) |
365 |
+ |
thread.join(); |
366 |
|
} |
367 |
|
|
368 |
|
/** |
369 |
|
* awaitAdvance returns the current phase |
370 |
|
*/ |
371 |
< |
public void testAwaitAdvance5() { |
371 |
> |
public void testAwaitAdvance5() throws InterruptedException { |
372 |
|
final Phaser phaser = new Phaser(1); |
373 |
|
int phase = phaser.awaitAdvance(phaser.arrive()); |
374 |
|
assertEquals(phase, phaser.getPhase()); |
375 |
|
phaser.register(); |
376 |
< |
for (int i = 0; i < eight; i++) { |
377 |
< |
new Thread() { |
378 |
< |
|
379 |
< |
public void run() { |
380 |
< |
getRunnable(SHORT_DELAY_MS).run(); |
376 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
377 |
> |
for (int i = 0; i < 8; i++) { |
378 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
379 |
> |
public void realRun() { |
380 |
> |
sleepTillInterrupted(SHORT_DELAY_MS); |
381 |
|
phaser.arrive(); |
382 |
< |
} |
409 |
< |
}.start(); |
382 |
> |
}})); |
383 |
|
phase = phaser.awaitAdvance(phaser.arrive()); |
384 |
|
assertEquals(phase, phaser.getPhase()); |
385 |
|
} |
386 |
+ |
for (Thread thread : threads) |
387 |
+ |
thread.join(); |
388 |
|
} |
389 |
|
|
390 |
|
/** |
391 |
|
* awaitAdvance returns when the phaser is externally terminated |
392 |
|
*/ |
393 |
< |
public void testAwaitAdvance6() { |
393 |
> |
public void testAwaitAdvance6() throws InterruptedException { |
394 |
|
final Phaser phaser = new Phaser(3); |
395 |
|
/* |
396 |
|
* Start new thread. This thread waits a small amount of time |
398 |
|
* in the main thread arrives quickly so at best this thread |
399 |
|
* waits for the second thread's party to arrive |
400 |
|
*/ |
401 |
< |
new Thread() { |
402 |
< |
|
403 |
< |
public void run() { |
429 |
< |
getRunnable(SMALL_DELAY_MS).run(); |
401 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
402 |
> |
public void realRun() { |
403 |
> |
sleepTillInterrupted(SMALL_DELAY_MS); |
404 |
|
int phase = phaser.awaitAdvance(phaser.arrive()); |
405 |
|
/* |
406 |
|
* This point is reached when force termination is called in which phase = -1 |
407 |
|
*/ |
408 |
< |
threadAssertTrue(phase < 0); |
409 |
< |
threadAssertTrue(phaser.isTerminated()); |
410 |
< |
} |
437 |
< |
}.start(); |
408 |
> |
assertTrue(phase < 0); |
409 |
> |
assertTrue(phaser.isTerminated()); |
410 |
> |
}}); |
411 |
|
/* |
412 |
|
* This thread will cause the first thread run to wait, in doing so |
413 |
< |
* the main thread will force termination in which the first thread |
414 |
< |
* should exit peacefully as this one |
413 |
> |
* the main thread will force termination in which the first thread |
414 |
> |
* should exit peacefully as this one |
415 |
|
*/ |
416 |
< |
new Thread() { |
417 |
< |
|
418 |
< |
public void run() { |
446 |
< |
getRunnable(LONG_DELAY_MS).run(); |
416 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
417 |
> |
public void realRun() { |
418 |
> |
sleepTillInterrupted(MEDIUM_DELAY_MS); |
419 |
|
int p1 = phaser.arrive(); |
420 |
|
int phase = phaser.awaitAdvance(p1); |
421 |
< |
threadAssertTrue(phase < 0); |
422 |
< |
threadAssertTrue(phaser.isTerminated()); |
423 |
< |
} |
452 |
< |
}.start(); |
421 |
> |
assertTrue(phase < 0); |
422 |
> |
assertTrue(phaser.isTerminated()); |
423 |
> |
}}); |
424 |
|
|
425 |
|
phaser.arrive(); |
426 |
|
phaser.forceTermination(); |
427 |
+ |
t1.join(); |
428 |
+ |
t2.join(); |
429 |
|
} |
430 |
|
|
431 |
|
/** |
437 |
|
Phaser phaser = new Phaser(); |
438 |
|
phaser.arriveAndAwaitAdvance(); |
439 |
|
shouldThrow(); |
440 |
< |
} catch (IllegalStateException success) { |
468 |
< |
} |
440 |
> |
} catch (IllegalStateException success) {} |
441 |
|
} |
442 |
|
|
443 |
|
/** |
444 |
|
* Interrupted arriveAndAwaitAdvance does not throw InterruptedException |
445 |
|
*/ |
446 |
< |
public void testArriveAndAwaitAdvance2() { |
446 |
> |
public void testArriveAndAwaitAdvance2() throws InterruptedException { |
447 |
|
final Phaser phaser = new Phaser(2); |
448 |
< |
Thread th = new Thread() { |
449 |
< |
public void run() { |
450 |
< |
try { |
451 |
< |
phaser.arriveAndAwaitAdvance(); |
452 |
< |
} catch (Exception failure) { |
453 |
< |
threadUnexpectedException(failure); |
454 |
< |
} |
455 |
< |
} |
456 |
< |
}; |
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 |
< |
} |
448 |
> |
Thread th = newStartedThread(new CheckedRunnable() { |
449 |
> |
public void realRun() { |
450 |
> |
phaser.arriveAndAwaitAdvance(); |
451 |
> |
}}); |
452 |
> |
|
453 |
> |
Thread.sleep(SMALL_DELAY_MS); |
454 |
> |
th.interrupt(); |
455 |
> |
Thread.sleep(SMALL_DELAY_MS); |
456 |
> |
phaser.arrive(); |
457 |
|
assertFalse(th.isInterrupted()); |
458 |
+ |
th.join(); |
459 |
|
} |
460 |
|
|
461 |
|
/** |
463 |
|
* number of arrived parties is the same number that is accounted |
464 |
|
* for when the main thread awaitsAdvance |
465 |
|
*/ |
466 |
< |
public void testArriveAndAwaitAdvance3() { |
466 |
> |
public void testArriveAndAwaitAdvance3() throws InterruptedException { |
467 |
|
final Phaser phaser = new Phaser(1); |
468 |
< |
final AtomicInteger arrivingCount = new AtomicInteger(0); |
469 |
< |
for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) { |
470 |
< |
new Thread() { |
471 |
< |
|
472 |
< |
public void run() { |
473 |
< |
phaser.register(); |
474 |
< |
run.run(); |
512 |
< |
arrivingCount.getAndIncrement(); |
513 |
< |
phaser.arrive(); |
514 |
< |
} |
515 |
< |
}.start(); |
468 |
> |
final List<Thread> threads = new ArrayList<Thread>(); |
469 |
> |
for (int i = 0; i < 3; i++) { |
470 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
471 |
> |
public void realRun() throws InterruptedException { |
472 |
> |
phaser.register(); |
473 |
> |
phaser.arriveAndAwaitAdvance(); |
474 |
> |
}})); |
475 |
|
} |
476 |
< |
int phaseNumber = phaser.arriveAndAwaitAdvance(); |
477 |
< |
arrivingCount.incrementAndGet(); |
478 |
< |
//the + 1 adds to expectedArrive to account for the main threads arrival |
479 |
< |
int expectedArrived = phaseNumber > 0 ? phaseNumber * six + 1 : phaser.getArrivedParties() + 1; |
480 |
< |
threadAssertEquals(expectedArrived, arrivingCount.get()); |
522 |
< |
} |
523 |
< |
// .. initially called, for n tasks via |
524 |
< |
private List<Runnable> getRunnables(int size, long wait) { |
525 |
< |
List<Runnable> list = new ArrayList<Runnable>(); |
526 |
< |
for (int i = 0; i < size; i++) { |
527 |
< |
list.add(getRunnable(wait)); |
528 |
< |
} |
529 |
< |
return list; |
530 |
< |
} |
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 |
< |
}; |
476 |
> |
Thread.sleep(LONG_DELAY_MS); |
477 |
> |
assertEquals(phaser.getArrivedParties(), 3); |
478 |
> |
phaser.arriveAndAwaitAdvance(); |
479 |
> |
for (Thread thread : threads) |
480 |
> |
thread.join(); |
481 |
|
} |
482 |
|
|
483 |
|
} |