Search in sources :

Example 11 with Phaser

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

the class PhaserTest method testArrive2.

/**
 * arriveAndDeregister does not wait for others to arrive at barrier
 */
public void testArrive2() {
    final Phaser phaser = new Phaser();
    assertEquals(0, phaser.register());
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        assertEquals(0, phaser.register());
        threads.add(newStartedThread(new CheckedRunnable() {

            public void realRun() {
                assertEquals(0, phaser.arriveAndDeregister());
            }
        }));
    }
    for (Thread thread : threads) awaitTermination(thread);
    assertState(phaser, 0, 1, 1);
    assertEquals(0, phaser.arrive());
    assertState(phaser, 1, 1, 1);
}
Also used : ArrayList(java.util.ArrayList) Phaser(java8.util.concurrent.Phaser)

Example 12 with Phaser

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

the class PhaserTest method testAwaitAdvance2.

/**
 * awaitAdvance with a negative parameter will return without affecting the
 * phaser
 */
public void testAwaitAdvance2() {
    Phaser phaser = new Phaser();
    assertTrue(phaser.awaitAdvance(-1) < 0);
    assertState(phaser, 0, 0, 0);
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 13 with Phaser

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

the class PhaserTest method testRegister4.

/**
 * register causes the next arrive to not increment the phase
 * rather retain the phase number
 */
public void testRegister4() {
    Phaser phaser = new Phaser(1);
    assertEquals(0, phaser.arrive());
    assertEquals(1, phaser.register());
    assertEquals(1, phaser.arrive());
    assertState(phaser, 1, 2, 1);
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 14 with Phaser

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

the class PhaserTest method testArriveAndAwaitAdvance1.

/**
 * arriveAndAwaitAdvance throws IllegalStateException with no
 * unarrived parties
 */
public void testArriveAndAwaitAdvance1() {
    Phaser phaser = new Phaser();
    try {
        phaser.arriveAndAwaitAdvance();
        shouldThrow();
    } catch (IllegalStateException success) {
    }
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 15 with Phaser

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

the class PhaserTest method testRegister3.

/**
 * register() correctly returns the current barrier phase number
 * when invoked
 */
public void testRegister3() {
    Phaser phaser = new Phaser();
    assertEquals(0, phaser.register());
    assertEquals(0, phaser.arrive());
    assertEquals(1, phaser.register());
    assertState(phaser, 1, 2, 2);
}
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