use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testArriveAndDeregister1.
/**
* arriveAndDeregister() throws IllegalStateException if number of
* registered or unarrived parties would become negative
*/
public void testArriveAndDeregister1() {
Phaser phaser = new Phaser();
try {
phaser.arriveAndDeregister();
shouldThrow();
} catch (IllegalStateException success) {
}
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testArriveAndAwaitAdvanceBeforeInterrupt.
/**
* arriveAndAwaitAdvance continues waiting if interrupted while waiting
*/
public void testArriveAndAwaitAdvanceBeforeInterrupt() {
final Phaser phaser = new Phaser();
assertEquals(0, phaser.register());
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
assertEquals(0, phaser.register());
assertFalse(Thread.currentThread().isInterrupted());
pleaseInterrupt.countDown();
assertEquals(1, phaser.arriveAndAwaitAdvance());
assertTrue(Thread.interrupted());
}
});
await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt();
Thread.currentThread().interrupt();
assertEquals(1, phaser.arriveAndAwaitAdvance());
assertTrue(Thread.interrupted());
awaitTermination(t);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testAwaitAdvanceInterruptibly_interruptible.
/**
* awaitAdvanceInterruptibly blocks interruptibly
*/
public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException {
final Phaser phaser = new Phaser(1);
final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() {
Thread.currentThread().interrupt();
try {
phaser.awaitAdvanceInterruptibly(0);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
pleaseInterrupt.countDown();
try {
phaser.awaitAdvanceInterruptibly(0);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
}
});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() throws TimeoutException {
Thread.currentThread().interrupt();
try {
phaser.awaitAdvanceInterruptibly(0, 2 * LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
pleaseInterrupt.countDown();
try {
phaser.awaitAdvanceInterruptibly(0, 2 * LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
}
});
await(pleaseInterrupt);
assertState(phaser, 0, 1, 1);
assertThreadBlocks(t1, Thread.State.WAITING);
assertThreadBlocks(t2, Thread.State.TIMED_WAITING);
t1.interrupt();
t2.interrupt();
awaitTermination(t1);
awaitTermination(t2);
assertState(phaser, 0, 1, 1);
assertEquals(0, phaser.arrive());
assertState(phaser, 1, 1, 1);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testAwaitAdvanceBeforeInterrupt.
/**
* awaitAdvance continues waiting if interrupted while waiting
*/
public void testAwaitAdvanceBeforeInterrupt() {
final Phaser phaser = new Phaser();
assertEquals(0, phaser.register());
final CountDownLatch pleaseArrive = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
assertEquals(0, phaser.register());
assertEquals(0, phaser.arrive());
assertFalse(Thread.currentThread().isInterrupted());
pleaseArrive.countDown();
assertEquals(1, phaser.awaitAdvance(0));
assertTrue(Thread.interrupted());
}
});
await(pleaseArrive);
assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt();
assertEquals(0, phaser.arrive());
awaitTermination(t);
Thread.currentThread().interrupt();
assertEquals(1, phaser.awaitAdvance(0));
assertTrue(Thread.interrupted());
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testArriveAndAwaitAdvanceAfterInterrupt.
/**
* arriveAndAwaitAdvance continues waiting if interrupted before waiting
*/
public void testArriveAndAwaitAdvanceAfterInterrupt() {
final Phaser phaser = new Phaser();
assertEquals(0, phaser.register());
final CountDownLatch pleaseArrive = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
Thread.currentThread().interrupt();
assertEquals(0, phaser.register());
pleaseArrive.countDown();
assertTrue(Thread.currentThread().isInterrupted());
assertEquals(1, phaser.arriveAndAwaitAdvance());
assertTrue(Thread.interrupted());
}
});
await(pleaseArrive);
assertThreadBlocks(t, Thread.State.WAITING);
Thread.currentThread().interrupt();
assertEquals(1, phaser.arriveAndAwaitAdvance());
assertTrue(Thread.interrupted());
awaitTermination(t);
}
Aggregations