386 |
|
} |
387 |
|
|
388 |
|
/** |
389 |
< |
* awaitAdvance continues waiting if interrupted |
389 |
> |
* awaitAdvance continues waiting if interrupted before waiting |
390 |
|
*/ |
391 |
< |
public void testAwaitAdvance3() throws InterruptedException { |
391 |
> |
public void testAwaitAdvanceAfterInterrupt() throws InterruptedException { |
392 |
|
final Phaser phaser = new Phaser(); |
393 |
|
assertEquals(0, phaser.register()); |
394 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
395 |
|
|
396 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
397 |
|
public void realRun() throws InterruptedException { |
398 |
+ |
Thread.currentThread().interrupt(); |
399 |
|
assertEquals(0, phaser.register()); |
400 |
+ |
assertEquals(0, phaser.arrive()); |
401 |
|
threadStarted.countDown(); |
402 |
< |
assertEquals(1, phaser.awaitAdvance(phaser.arrive())); |
402 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
403 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
404 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
405 |
> |
}}); |
406 |
> |
|
407 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
408 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
409 |
> |
assertEquals(0, phaser.arrive()); |
410 |
> |
awaitTermination(t, SMALL_DELAY_MS); |
411 |
> |
|
412 |
> |
Thread.currentThread().interrupt(); |
413 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
414 |
> |
assertTrue(Thread.interrupted()); |
415 |
> |
} |
416 |
> |
|
417 |
> |
/** |
418 |
> |
* awaitAdvance continues waiting if interrupted while waiting |
419 |
> |
*/ |
420 |
> |
public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException { |
421 |
> |
final Phaser phaser = new Phaser(); |
422 |
> |
assertEquals(0, phaser.register()); |
423 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
424 |
> |
|
425 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
426 |
> |
public void realRun() throws InterruptedException { |
427 |
> |
assertEquals(0, phaser.register()); |
428 |
> |
assertEquals(0, phaser.arrive()); |
429 |
> |
threadStarted.countDown(); |
430 |
> |
assertFalse(Thread.currentThread().isInterrupted()); |
431 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
432 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
433 |
|
}}); |
434 |
+ |
|
435 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
436 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
437 |
|
t.interrupt(); |
438 |
|
assertEquals(0, phaser.arrive()); |
439 |
|
awaitTermination(t, SMALL_DELAY_MS); |
440 |
+ |
|
441 |
+ |
Thread.currentThread().interrupt(); |
442 |
+ |
assertEquals(1, phaser.awaitAdvance(0)); |
443 |
+ |
assertTrue(Thread.interrupted()); |
444 |
+ |
} |
445 |
+ |
|
446 |
+ |
/** |
447 |
+ |
* arriveAndAwaitAdvance continues waiting if interrupted before waiting |
448 |
+ |
*/ |
449 |
+ |
public void testArriveAndAwaitAdvanceAfterInterrupt() |
450 |
+ |
throws InterruptedException { |
451 |
+ |
final Phaser phaser = new Phaser(); |
452 |
+ |
assertEquals(0, phaser.register()); |
453 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
454 |
+ |
|
455 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
456 |
+ |
public void realRun() throws InterruptedException { |
457 |
+ |
Thread.currentThread().interrupt(); |
458 |
+ |
assertEquals(0, phaser.register()); |
459 |
+ |
threadStarted.countDown(); |
460 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
461 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
462 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
463 |
+ |
}}); |
464 |
+ |
|
465 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
466 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
467 |
+ |
Thread.currentThread().interrupt(); |
468 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
469 |
+ |
assertTrue(Thread.interrupted()); |
470 |
+ |
awaitTermination(t, SMALL_DELAY_MS); |
471 |
+ |
} |
472 |
+ |
|
473 |
+ |
/** |
474 |
+ |
* arriveAndAwaitAdvance continues waiting if interrupted while waiting |
475 |
+ |
*/ |
476 |
+ |
public void testArriveAndAwaitAdvanceBeforeInterrupt() |
477 |
+ |
throws InterruptedException { |
478 |
+ |
final Phaser phaser = new Phaser(); |
479 |
+ |
assertEquals(0, phaser.register()); |
480 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
481 |
+ |
|
482 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
483 |
+ |
public void realRun() throws InterruptedException { |
484 |
+ |
assertEquals(0, phaser.register()); |
485 |
+ |
threadStarted.countDown(); |
486 |
+ |
assertFalse(Thread.currentThread().isInterrupted()); |
487 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
488 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
489 |
+ |
}}); |
490 |
+ |
|
491 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
492 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
493 |
+ |
t.interrupt(); |
494 |
+ |
Thread.currentThread().interrupt(); |
495 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
496 |
+ |
assertTrue(Thread.interrupted()); |
497 |
+ |
awaitTermination(t, SMALL_DELAY_MS); |
498 |
|
} |
499 |
|
|
500 |
|
/** |
592 |
|
} |
593 |
|
|
594 |
|
/** |
504 |
– |
* Interrupted arriveAndAwaitAdvance does not throw InterruptedException |
505 |
– |
*/ |
506 |
– |
public void testArriveAndAwaitAdvance2() throws InterruptedException { |
507 |
– |
final Phaser phaser = new Phaser(2); |
508 |
– |
final CountDownLatch threadStarted = new CountDownLatch(1); |
509 |
– |
final AtomicBoolean advanced = new AtomicBoolean(false); |
510 |
– |
final AtomicBoolean checkedInterruptStatus = new AtomicBoolean(false); |
511 |
– |
Thread t = newStartedThread(new CheckedRunnable() { |
512 |
– |
public void realRun() throws InterruptedException { |
513 |
– |
threadStarted.countDown(); |
514 |
– |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
515 |
– |
assertState(phaser, 1, 2, 2); |
516 |
– |
advanced.set(true); |
517 |
– |
assertTrue(Thread.currentThread().isInterrupted()); |
518 |
– |
while (!checkedInterruptStatus.get()) |
519 |
– |
Thread.yield(); |
520 |
– |
}}); |
521 |
– |
|
522 |
– |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
523 |
– |
t.interrupt(); |
524 |
– |
assertEquals(0, phaser.arrive()); |
525 |
– |
while (!advanced.get()) |
526 |
– |
Thread.yield(); |
527 |
– |
assertTrue(t.isInterrupted()); |
528 |
– |
checkedInterruptStatus.set(true); |
529 |
– |
awaitTermination(t, SMALL_DELAY_MS); |
530 |
– |
} |
531 |
– |
|
532 |
– |
/** |
595 |
|
* arriveAndAwaitAdvance waits for all threads to arrive, the |
596 |
|
* number of arrived parties is the same number that is accounted |
597 |
|
* for when the main thread awaitsAdvance |