40 |
|
public void testConstructor2() { |
41 |
|
try { |
42 |
|
new Phaser(-1); |
43 |
< |
this.shouldThrow(); |
43 |
> |
shouldThrow(); |
44 |
|
} catch (IllegalArgumentException success) { |
45 |
|
} |
46 |
|
} |
101 |
|
phaser.register(); |
102 |
|
shouldThrow(); |
103 |
|
} catch (IllegalStateException success) { |
104 |
– |
} catch (Exception ex) { |
105 |
– |
threadUnexpectedException(ex); |
104 |
|
} |
105 |
|
} |
106 |
|
|
190 |
|
} |
191 |
|
|
192 |
|
/** |
193 |
< |
* arrive does not wait for others to arrive at barrier |
193 |
> |
* arriveAndDeregister does not wait for others to arrive at barrier |
194 |
|
*/ |
195 |
< |
public void testArrive2() { |
195 |
> |
public void testArrive2() throws InterruptedException { |
196 |
|
final Phaser phaser = new Phaser(1); |
197 |
|
phaser.register(); |
198 |
< |
Thread thread = null; |
199 |
< |
for (final Runnable r : getRunnables(10, SHORT_DELAY_MS)) { |
198 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
199 |
> |
for (int i = 0; i < 10; i++) |
200 |
|
phaser.register(); |
201 |
< |
thread = new Thread() { |
202 |
< |
|
203 |
< |
public void run() { |
206 |
< |
r.run(); |
201 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
202 |
> |
void realRun() throws InterruptedException { |
203 |
> |
Thread.sleep(SMALL_DELAY_MS); |
204 |
|
phaser.arriveAndDeregister(); |
205 |
< |
} |
209 |
< |
}; |
210 |
< |
thread.start(); |
211 |
< |
} |
205 |
> |
}})); |
206 |
|
|
207 |
|
phaser.arrive(); |
208 |
< |
assertTrue(thread.isAlive()); |
208 |
> |
assertTrue(threads.get(0).isAlive()); |
209 |
|
assertFalse(phaser.isTerminated()); |
210 |
+ |
for (Thread thread : threads) |
211 |
+ |
thread.join(); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
218 |
|
Phaser phaser = new Phaser(1); |
219 |
|
phaser.forceTermination(); |
220 |
|
assertTrue(phaser.arrive() < 0); |
225 |
– |
|
221 |
|
} |
222 |
|
|
223 |
|
/** |
229 |
|
Phaser phaser = new Phaser(); |
230 |
|
phaser.arriveAndDeregister(); |
231 |
|
shouldThrow(); |
237 |
– |
|
232 |
|
} catch (IllegalStateException success) { |
233 |
|
} |
234 |
|
} |
298 |
|
* arriveAndDeregister returns the phase in which it leaves the |
299 |
|
* phaser in after deregistration |
300 |
|
*/ |
301 |
< |
public void testArriveAndDeregister6() { |
301 |
> |
public void testArriveAndDeregister6() throws InterruptedException { |
302 |
|
final Phaser phaser = new Phaser(2); |
303 |
< |
new Thread() { |
304 |
< |
|
305 |
< |
public void run() { |
312 |
< |
getRunnable(SHORT_DELAY_MS).run(); |
303 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
304 |
> |
void realRun() { |
305 |
> |
sleepTillInterrupted(SHORT_DELAY_MS); |
306 |
|
phaser.arrive(); |
307 |
< |
} |
315 |
< |
}.start(); |
307 |
> |
}}); |
308 |
|
phaser.arriveAndAwaitAdvance(); |
309 |
|
int phase = phaser.arriveAndDeregister(); |
310 |
|
assertEquals(phase, phaser.getPhase()); |
311 |
+ |
t.join(); |
312 |
|
} |
313 |
|
|
314 |
|
/** |
324 |
|
* phaser |
325 |
|
*/ |
326 |
|
public void testAwaitAdvance2() { |
327 |
< |
try { |
328 |
< |
Phaser phaser = new Phaser(); |
336 |
< |
phaser.awaitAdvance(-1); |
337 |
< |
} catch (Exception failure) { |
338 |
< |
this.unexpectedException(); |
339 |
< |
} |
327 |
> |
Phaser phaser = new Phaser(); |
328 |
> |
phaser.awaitAdvance(-1); |
329 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
|
* awaitAdvance while waiting does not abort on interrupt. |
333 |
|
*/ |
334 |
< |
public void testAwaitAdvance3() { |
334 |
> |
public void testAwaitAdvance3() throws InterruptedException { |
335 |
|
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 |
– |
}; |
336 |
|
phaser.register(); |
337 |
< |
th1.start(); |
338 |
< |
try { |
339 |
< |
Thread.sleep(SHORT_DELAY_MS); |
340 |
< |
th1.interrupt(); |
341 |
< |
Thread.sleep(LONG_DELAY_MS); |
342 |
< |
phaser.arrive(); |
343 |
< |
} catch (Exception failure) { |
344 |
< |
unexpectedException(); |
345 |
< |
} |
346 |
< |
assertFalse(th1.isInterrupted()); |
337 |
> |
|
338 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
339 |
> |
void realRun() throws InterruptedException { |
340 |
> |
phaser.register(); |
341 |
> |
sleepTillInterrupted(LONG_DELAY_MS); |
342 |
> |
phaser.awaitAdvance(phaser.arrive()); |
343 |
> |
}}); |
344 |
> |
Thread.sleep(SMALL_DELAY_MS); |
345 |
> |
t.interrupt(); |
346 |
> |
Thread.sleep(SMALL_DELAY_MS); |
347 |
> |
phaser.arrive(); |
348 |
> |
assertFalse(t.isInterrupted()); |
349 |
> |
t.join(); |
350 |
|
} |
351 |
|
|
352 |
|
/** |
353 |
|
* awaitAdvance atomically waits for all parties within the same phase to |
354 |
|
* complete before continuing |
355 |
|
*/ |
356 |
< |
public void testAwaitAdvance4() { |
357 |
< |
final Phaser phaser = new Phaser(four); |
356 |
> |
public void testAwaitAdvance4() throws InterruptedException { |
357 |
> |
final Phaser phaser = new Phaser(4); |
358 |
|
final AtomicInteger phaseCount = new AtomicInteger(0); |
359 |
< |
for (int i = 0; i < four; i++) { |
360 |
< |
new Thread() { |
361 |
< |
|
362 |
< |
public void run() { |
359 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
360 |
> |
for (int i = 0; i < 4; i++) { |
361 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
362 |
> |
void realRun() { |
363 |
|
int phase = phaser.arrive(); |
364 |
|
phaseCount.incrementAndGet(); |
365 |
< |
getRunnable(LONG_DELAY_MS).run(); |
365 |
> |
sleepTillInterrupted(SMALL_DELAY_MS); |
366 |
|
phaser.awaitAdvance(phase); |
367 |
< |
assertTrue(phaseCount.get() == four); |
368 |
< |
} |
390 |
< |
}.start(); |
367 |
> |
threadAssertTrue(phaseCount.get() == 4); |
368 |
> |
}})); |
369 |
|
} |
370 |
+ |
for (Thread thread : threads) |
371 |
+ |
thread.join(); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
|
* awaitAdvance returns the current phase |
376 |
|
*/ |
377 |
< |
public void testAwaitAdvance5() { |
377 |
> |
public void testAwaitAdvance5() throws InterruptedException { |
378 |
|
final Phaser phaser = new Phaser(1); |
379 |
|
int phase = phaser.awaitAdvance(phaser.arrive()); |
380 |
|
assertEquals(phase, phaser.getPhase()); |
381 |
|
phaser.register(); |
382 |
< |
for (int i = 0; i < eight; i++) { |
383 |
< |
new Thread() { |
384 |
< |
|
385 |
< |
public void run() { |
386 |
< |
getRunnable(SHORT_DELAY_MS).run(); |
382 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
383 |
> |
for (int i = 0; i < 8; i++) { |
384 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
385 |
> |
void realRun() { |
386 |
> |
sleepTillInterrupted(SHORT_DELAY_MS); |
387 |
|
phaser.arrive(); |
388 |
< |
} |
409 |
< |
}.start(); |
388 |
> |
}})); |
389 |
|
phase = phaser.awaitAdvance(phaser.arrive()); |
390 |
< |
assertEquals(phase, phaser.getPhase()); |
390 |
> |
threadAssertEquals(phase, phaser.getPhase()); |
391 |
|
} |
392 |
+ |
for (Thread thread : threads) |
393 |
+ |
thread.join(); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
397 |
|
* awaitAdvance returns when the phaser is externally terminated |
398 |
|
*/ |
399 |
< |
public void testAwaitAdvance6() { |
399 |
> |
public void testAwaitAdvance6() throws InterruptedException { |
400 |
|
final Phaser phaser = new Phaser(3); |
401 |
|
/* |
402 |
|
* Start new thread. This thread waits a small amount of time |
404 |
|
* in the main thread arrives quickly so at best this thread |
405 |
|
* waits for the second thread's party to arrive |
406 |
|
*/ |
407 |
< |
new Thread() { |
408 |
< |
|
409 |
< |
public void run() { |
429 |
< |
getRunnable(SMALL_DELAY_MS).run(); |
407 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
408 |
> |
void realRun() { |
409 |
> |
sleepTillInterrupted(SMALL_DELAY_MS); |
410 |
|
int phase = phaser.awaitAdvance(phaser.arrive()); |
411 |
|
/* |
412 |
|
* This point is reached when force termination is called in which phase = -1 |
413 |
|
*/ |
414 |
|
threadAssertTrue(phase < 0); |
415 |
|
threadAssertTrue(phaser.isTerminated()); |
416 |
< |
} |
437 |
< |
}.start(); |
416 |
> |
}}); |
417 |
|
/* |
418 |
|
* This thread will cause the first thread run to wait, in doing so |
419 |
|
* the main thread will force termination in which the first thread |
420 |
|
* should exit peacefully as this one |
421 |
|
*/ |
422 |
< |
new Thread() { |
423 |
< |
|
424 |
< |
public void run() { |
446 |
< |
getRunnable(LONG_DELAY_MS).run(); |
422 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
423 |
> |
void realRun() { |
424 |
> |
sleepTillInterrupted(MEDIUM_DELAY_MS); |
425 |
|
int p1 = phaser.arrive(); |
426 |
|
int phase = phaser.awaitAdvance(p1); |
427 |
|
threadAssertTrue(phase < 0); |
428 |
|
threadAssertTrue(phaser.isTerminated()); |
429 |
< |
} |
452 |
< |
}.start(); |
429 |
> |
}}); |
430 |
|
|
431 |
|
phaser.arrive(); |
432 |
|
phaser.forceTermination(); |
433 |
+ |
t1.join(); |
434 |
+ |
t2.join(); |
435 |
|
} |
436 |
|
|
437 |
|
/** |
450 |
|
/** |
451 |
|
* Interrupted arriveAndAwaitAdvance does not throw InterruptedException |
452 |
|
*/ |
453 |
< |
public void testArriveAndAwaitAdvance2() { |
453 |
> |
public void testArriveAndAwaitAdvance2() throws InterruptedException { |
454 |
|
final Phaser phaser = new Phaser(2); |
455 |
< |
Thread th = new Thread() { |
456 |
< |
public void run() { |
457 |
< |
try { |
458 |
< |
phaser.arriveAndAwaitAdvance(); |
459 |
< |
} catch (Exception failure) { |
460 |
< |
threadUnexpectedException(failure); |
461 |
< |
} |
462 |
< |
} |
463 |
< |
}; |
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 |
< |
} |
455 |
> |
Thread th = newStartedThread(new CheckedRunnable() { |
456 |
> |
void realRun() { |
457 |
> |
phaser.arriveAndAwaitAdvance(); |
458 |
> |
}}); |
459 |
> |
|
460 |
> |
Thread.sleep(SMALL_DELAY_MS); |
461 |
> |
th.interrupt(); |
462 |
> |
Thread.sleep(SMALL_DELAY_MS); |
463 |
> |
phaser.arrive(); |
464 |
|
assertFalse(th.isInterrupted()); |
465 |
+ |
th.join(); |
466 |
|
} |
467 |
|
|
468 |
|
/** |
470 |
|
* number of arrived parties is the same number that is accounted |
471 |
|
* for when the main thread awaitsAdvance |
472 |
|
*/ |
473 |
< |
public void testArriveAndAwaitAdvance3() { |
473 |
> |
public void testArriveAndAwaitAdvance3() throws InterruptedException { |
474 |
|
final Phaser phaser = new Phaser(1); |
475 |
|
final AtomicInteger arrivingCount = new AtomicInteger(0); |
476 |
< |
for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) { |
477 |
< |
new Thread() { |
478 |
< |
|
479 |
< |
public void run() { |
476 |
> |
final List<Thread> threads = new ArrayList<Thread>(); |
477 |
> |
for (int i = 0; i < 6; i++) { |
478 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
479 |
> |
void realRun() throws InterruptedException { |
480 |
|
phaser.register(); |
481 |
< |
run.run(); |
481 |
> |
sleepTillInterrupted(SHORT_DELAY_MS); |
482 |
|
arrivingCount.getAndIncrement(); |
483 |
|
phaser.arrive(); |
484 |
< |
} |
515 |
< |
}.start(); |
484 |
> |
}})); |
485 |
|
} |
486 |
|
int phaseNumber = phaser.arriveAndAwaitAdvance(); |
487 |
|
arrivingCount.incrementAndGet(); |
488 |
|
//the + 1 adds to expectedArrive to account for the main threads arrival |
489 |
|
int expectedArrived = phaseNumber > 0 ? phaseNumber * six + 1 : phaser.getArrivedParties() + 1; |
490 |
|
threadAssertEquals(expectedArrived, arrivingCount.get()); |
491 |
< |
} |
492 |
< |
// .. 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 |
< |
}; |
491 |
> |
for (Thread thread : threads) |
492 |
> |
thread.join(); |
493 |
|
} |
494 |
|
|
495 |
|
} |