use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testRegister1.
/**
* register() will increment the number of unarrived parties by
* one and not affect its arrived parties
*/
public void testRegister1() {
Phaser phaser = new Phaser();
assertState(phaser, 0, 0, 0);
assertEquals(0, phaser.register());
assertState(phaser, 0, 1, 1);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testArriveAndDeregister6.
/**
* arriveAndDeregister returns the phase in which it leaves the
* phaser in after deregistration
*/
public void testArriveAndDeregister6() {
final Phaser phaser = new Phaser(2);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
assertEquals(0, phaser.arrive());
}
});
assertEquals(1, phaser.arriveAndAwaitAdvance());
assertState(phaser, 1, 2, 2);
assertEquals(1, phaser.arriveAndDeregister());
assertState(phaser, 1, 1, 1);
assertEquals(1, phaser.arriveAndDeregister());
assertTerminated(phaser, 2);
awaitTermination(t);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testArriveAndAwaitAdvance3.
/**
* arriveAndAwaitAdvance waits for all threads to arrive, the
* number of arrived parties is the same number that is accounted
* for when the main thread awaitsAdvance
*/
public void testArriveAndAwaitAdvance3() {
final Phaser phaser = new Phaser(1);
final int THREADS = 3;
final CountDownLatch pleaseArrive = new CountDownLatch(THREADS);
final List<Thread> threads = new ArrayList<>();
for (int i = 0; i < THREADS; i++) threads.add(newStartedThread(new CheckedRunnable() {
public void realRun() {
assertEquals(0, phaser.register());
pleaseArrive.countDown();
assertEquals(1, phaser.arriveAndAwaitAdvance());
}
}));
await(pleaseArrive);
long startTime = System.nanoTime();
while (phaser.getArrivedParties() < THREADS) Thread.yield();
assertEquals(THREADS, phaser.getArrivedParties());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
for (Thread thread : threads) assertThreadBlocks(thread, Thread.State.WAITING);
for (Thread thread : threads) assertTrue(thread.isAlive());
assertState(phaser, 0, THREADS + 1, 1);
phaser.arriveAndAwaitAdvance();
for (Thread thread : threads) awaitTermination(thread);
assertState(phaser, 1, THREADS + 1, THREADS + 1);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testAwaitAdvanceAfterInterrupt.
/**
* awaitAdvance continues waiting if interrupted before waiting
*/
public void testAwaitAdvanceAfterInterrupt() {
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());
assertEquals(0, phaser.arrive());
pleaseArrive.countDown();
assertTrue(Thread.currentThread().isInterrupted());
assertEquals(1, phaser.awaitAdvance(0));
assertTrue(Thread.interrupted());
}
});
await(pleaseArrive);
assertThreadBlocks(t, Thread.State.WAITING);
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 testArriveAndDeregister.
/**
* arriveAndDeregister does not wait for others to arrive at barrier
*/
public void testArriveAndDeregister() {
final Phaser phaser = new Phaser(1);
for (int i = 0; i < 10; i++) {
assertState(phaser, 0, 1, 1);
assertEquals(0, phaser.register());
assertState(phaser, 0, 2, 2);
assertEquals(0, phaser.arriveAndDeregister());
assertState(phaser, 0, 1, 1);
}
assertEquals(0, phaser.arriveAndDeregister());
assertTerminated(phaser, 1);
}
Aggregations