use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testConstructorNegativeParties2.
/**
* Constructing with a negative number of parties throws
* IllegalArgumentException
*/
public void testConstructorNegativeParties2() {
try {
new Phaser(new Phaser(), -1);
shouldThrow();
} catch (IllegalArgumentException success) {
}
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testAwaitAdvance4.
/**
* awaitAdvance atomically waits for all parties within the same phase to
* complete before continuing
*/
public void testAwaitAdvance4() {
final Phaser phaser = new Phaser(4);
final AtomicInteger count = new AtomicInteger(0);
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 4; i++) threads.add(newStartedThread(new CheckedRunnable() {
public void realRun() {
for (int k = 0; k < 3; k++) {
assertEquals(2 * k + 1, phaser.arriveAndAwaitAdvance());
count.incrementAndGet();
assertEquals(2 * k + 1, phaser.arrive());
assertEquals(2 * k + 2, phaser.awaitAdvance(2 * k + 1));
assertEquals(4 * (k + 1), count.get());
}
}
}));
for (Thread thread : threads) awaitTermination(thread);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testAwaitAdvance1.
/**
* awaitAdvance succeeds upon advance
*/
public void testAwaitAdvance1() {
final Phaser phaser = new Phaser(1);
assertEquals(0, phaser.arrive());
assertEquals(1, phaser.awaitAdvance(0));
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testRegister2.
/**
* Registering more than 65536 parties causes IllegalStateException
*/
public void testRegister2() {
Phaser phaser = new Phaser(0);
assertState(phaser, 0, 0, 0);
assertEquals(0, phaser.bulkRegister(maxParties - 10));
assertState(phaser, 0, maxParties - 10, maxParties - 10);
for (int i = 0; i < 10; i++) {
assertState(phaser, 0, maxParties - 10 + i, maxParties - 10 + i);
assertEquals(0, phaser.register());
}
assertState(phaser, 0, maxParties, maxParties);
try {
phaser.register();
shouldThrow();
} catch (IllegalStateException success) {
}
try {
phaser.bulkRegister(Integer.MAX_VALUE);
shouldThrow();
} catch (IllegalStateException success) {
}
assertEquals(0, phaser.bulkRegister(0));
assertState(phaser, 0, maxParties, maxParties);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testArriveAndDeregister2.
/**
* arriveAndDeregister reduces the number of arrived parties
*/
public void testArriveAndDeregister2() {
final Phaser phaser = new Phaser(1);
assertEquals(0, phaser.register());
assertEquals(0, phaser.arrive());
assertState(phaser, 0, 2, 1);
assertEquals(0, phaser.arriveAndDeregister());
assertState(phaser, 1, 1, 1);
}
Aggregations