Search in sources :

Example 21 with Phaser

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);
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 22 with Phaser

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);
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 23 with Phaser

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());
    }
}
Also used : ArrayList(java.util.ArrayList) Phaser(java8.util.concurrent.Phaser)

Example 24 with Phaser

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();
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 25 with Phaser

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);
}
Also used : Phaser(java8.util.concurrent.Phaser)

Aggregations

Phaser (java8.util.concurrent.Phaser)51 CountDownLatch (java.util.concurrent.CountDownLatch)9 ArrayList (java.util.ArrayList)8 ExecutorService (java.util.concurrent.ExecutorService)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 LinkedList (java.util.LinkedList)2 Executors (java.util.concurrent.Executors)2 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Test (org.testng.annotations.Test)2 Array (java.lang.reflect.Array)1 ArrayDeque (java.util.ArrayDeque)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 Deque (java.util.Deque)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1