Search in sources :

Example 1 with Phaser

use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.

the class DoubleAccumulatorTest method testAccumulateAndGetMT.

/**
 * accumulates by multiple threads produce correct result
 */
public void testAccumulateAndGetMT() {
    final DoubleAccumulator acc = new DoubleAccumulator((x, y) -> x + y, 0.0);
    final int nThreads = ThreadLocalRandom.current().nextInt(1, 5);
    final Phaser phaser = new Phaser(nThreads + 1);
    final int incs = 1_000_000;
    // Gauss
    final double total = nThreads * incs / 2.0 * (incs - 1);
    final Runnable task = () -> {
        phaser.arriveAndAwaitAdvance();
        for (int i = 0; i < incs; i++) {
            acc.accumulate((double) i);
            assertTrue(acc.get() <= total);
        }
        phaser.arrive();
    };
    final ExecutorService p = Executors.newCachedThreadPool();
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(p);
        for (int i = nThreads; i-- > 0; ) /**/
        p.execute(task);
        phaser.arriveAndAwaitAdvance();
        phaser.arriveAndAwaitAdvance();
        assertEquals(total, acc.get());
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : DoubleAccumulator(java8.util.concurrent.atomic.DoubleAccumulator) ExecutorService(java.util.concurrent.ExecutorService) Phaser(java8.util.concurrent.Phaser)

Example 2 with Phaser

use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.

the class PhaserTest method testArriveAndDeregister4.

/**
 * arriveAndDeregister deregisters one party from its parent when
 * the number of parties of child is zero after deregistration
 */
public void testArriveAndDeregister4() {
    Phaser parent = new Phaser();
    Phaser child = new Phaser(parent);
    assertEquals(0, parent.register());
    assertEquals(0, child.register());
    assertState(child, 0, 1, 1);
    assertState(parent, 0, 2, 2);
    assertEquals(0, child.arriveAndDeregister());
    assertState(child, 0, 0, 0);
    assertState(parent, 0, 1, 1);
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 3 with Phaser

use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.

the class PhaserTest method testPhaseIncrement1.

/**
 * the phase number increments correctly when tripping the barrier
 */
public void testPhaseIncrement1() {
    for (int size = 1; size < nine; size++) {
        final Phaser phaser = new Phaser(size);
        for (int index = 0; index <= (1 << size); index++) {
            int phase = phaser.arrive();
            assertTrue(index % size == 0 ? (index / size) == phase : index - (phase * size) > 0);
        }
    }
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 4 with Phaser

use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.

the class PhaserTest method testArrive3.

/**
 * arrive() returns a negative number if the Phaser is terminated
 */
public void testArrive3() {
    Phaser phaser = new Phaser(1);
    phaser.forceTermination();
    assertTerminated(phaser, 0, 1);
    assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
    assertTrue(phaser.arrive() < 0);
    assertTrue(phaser.register() < 0);
    assertTrue(phaser.arriveAndDeregister() < 0);
    assertTrue(phaser.awaitAdvance(1) < 0);
    assertTrue(phaser.getPhase() < 0);
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 5 with Phaser

use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.

the class PhaserTest method testAwaitAdvance6.

/**
 * awaitAdvance returns when the phaser is externally terminated
 */
public void testAwaitAdvance6() {
    final Phaser phaser = new Phaser(3);
    final CountDownLatch pleaseForceTermination = new CountDownLatch(2);
    final List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < 2; i++) {
        Runnable r = new CheckedRunnable() {

            public void realRun() {
                assertEquals(0, phaser.arrive());
                pleaseForceTermination.countDown();
                assertTrue(phaser.awaitAdvance(0) < 0);
                assertTrue(phaser.isTerminated());
                assertTrue(phaser.getPhase() < 0);
                assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
                assertEquals(3, phaser.getRegisteredParties());
            }
        };
        threads.add(newStartedThread(r));
    }
    await(pleaseForceTermination);
    phaser.forceTermination();
    assertTrue(phaser.isTerminated());
    assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
    for (Thread thread : threads) awaitTermination(thread);
    assertEquals(3, phaser.getRegisteredParties());
}
Also used : ArrayList(java.util.ArrayList) Phaser(java8.util.concurrent.Phaser) CountDownLatch(java.util.concurrent.CountDownLatch)

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