use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaseOverflow method xtestTiered.
void xtestTiered() throws Throwable {
Phaser root = new Phaser();
stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
checkState(root, Integer.MAX_VALUE - 1, 0, 0);
Phaser p1 = new Phaser(root, 1);
checkState(root, Integer.MAX_VALUE - 1, 1, 1);
checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
Phaser p2 = new Phaser(root, 2);
checkState(root, Integer.MAX_VALUE - 1, 2, 2);
checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
int ph = Integer.MAX_VALUE - 1;
for (int k = 0; k < 5; k++) {
checkState(root, ph, 2, 2);
checkState(p1, ph, 1, 1);
checkState(p2, ph, 2, 2);
p1.arrive();
checkState(root, ph, 2, 1);
checkState(p1, ph, 1, 0);
checkState(p2, ph, 2, 2);
p2.arrive();
checkState(root, ph, 2, 1);
checkState(p1, ph, 1, 0);
checkState(p2, ph, 2, 1);
p2.arrive();
ph = phaseInc(ph);
checkState(root, ph, 2, 2);
checkState(p1, ph, 1, 1);
checkState(p2, ph, 2, 2);
}
equal(3, ph);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaseOverflow method testTiered.
void testTiered() throws Throwable {
Phaser root = new Phaser();
// this is extremely dependent on internal representation
stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
checkState(root, Integer.MAX_VALUE - 1, 0, 0);
Phaser p1 = new Phaser(root, 1);
checkState(root, Integer.MAX_VALUE - 1, 1, 1);
checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
Phaser p2 = new Phaser(root, 2);
checkState(root, Integer.MAX_VALUE - 1, 2, 2);
checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
int ph = Integer.MAX_VALUE - 1;
for (int k = 0; k < 5; k++) {
checkState(root, ph, 2, 2);
checkState(p1, ph, 1, 1);
checkState(p2, ph, 2, 2);
p1.arrive();
checkState(root, ph, 2, 1);
checkState(p1, ph, 1, 0);
checkState(p2, ph, 2, 2);
p2.arrive();
checkState(root, ph, 2, 1);
checkState(p1, ph, 1, 0);
checkState(p2, ph, 2, 1);
p2.arrive();
ph = phaseInc(ph);
checkState(root, ph, 2, 2);
checkState(p1, ph, 1, 1);
checkState(p2, ph, 2, 2);
}
equal(3, ph);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class FickleRegister method test.
void test(String[] args) throws Throwable {
final Phaser parent = new Phaser() {
protected boolean onAdvance(int phase, int parties) {
return false;
}
};
final Phaser child1 = new Phaser(parent);
final Phaser child2 = new Phaser(parent);
final Phaser subchild1 = new Phaser(child1);
final Phaser subchild2 = new Phaser(child2);
final Phaser[] phasers = { parent, child1, child2, subchild1, subchild2 };
int reps = 4;
ArrayList<Thread> threads = new ArrayList<>();
for (int j = 0; j < reps; ++j) {
threads.add(new Thread(new Runner(subchild1)));
threads.add(new Thread(new Runner(child1)));
threads.add(new Thread(new Runner(parent)));
threads.add(new Thread(new Runner(child2)));
threads.add(new Thread(new Runner(subchild2)));
}
for (Thread thread : threads) thread.start();
for (Thread thread : threads) thread.join();
System.out.println("Parent: " + parent);
System.out.println("Child1: " + child1);
System.out.println("Child2: " + child2);
System.out.println("Subchild1: " + subchild1);
System.out.println("Subchild2: " + subchild2);
System.out.println("Iterations:" + count.get());
for (Phaser phaser : phasers) {
check(phaser.getPhase() > 0);
equal(0, phaser.getRegisteredParties());
equal(0, phaser.getUnarrivedParties());
equal(parent.getPhase(), phaser.getPhase());
}
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class TieredArriveLoops method test.
void test(String[] args) throws Throwable {
final Phaser parent = new Phaser();
final Phaser child1 = new Phaser(parent);
final Phaser child2 = new Phaser(parent);
Thread t1 = new Thread(runner(child1));
Thread t2 = new Thread(runner(child2));
t1.start();
t2.start();
for (int prevPhase = 0, phase; ; prevPhase = phase) {
phase = child2.getPhase();
check(phase >= prevPhase);
if (System.nanoTime() - quittingTimeNanos > 0) {
System.err.printf("phase=%d%n", phase);
child1.forceTermination();
break;
}
}
t1.join();
t2.join();
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaseOverflow method testLeaf.
void testLeaf() throws Throwable {
Phaser phaser = new Phaser();
// this is extremely dependent on internal representation
stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
phaser.register();
checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
phaser.arrive();
checkState(phaser, Integer.MAX_VALUE, 1, 1);
phaser.arrive();
checkState(phaser, 0, 1, 1);
phaser.arrive();
checkState(phaser, 1, 1, 1);
}
Aggregations