Search in sources :

Example 36 with Phaser

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

the class PhaserTest method testConstructorPartiesExceedsLimit.

/**
 * Constructing with a number of parties > 65535 throws
 * IllegalArgumentException
 */
public void testConstructorPartiesExceedsLimit() {
    new Phaser(maxParties);
    try {
        new Phaser(maxParties + 1);
        shouldThrow();
    } catch (IllegalArgumentException success) {
    }
    new Phaser(new Phaser(), maxParties);
    try {
        new Phaser(new Phaser(), maxParties + 1);
        shouldThrow();
    } catch (IllegalArgumentException success) {
    }
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 37 with Phaser

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

the class PhaserTest method testRegisterEmptySubPhaser.

/**
 * register on a subphaser that is currently empty succeeds, even
 * in the presence of another non-empty subphaser
 */
public void testRegisterEmptySubPhaser() {
    Phaser root = new Phaser();
    Phaser child1 = new Phaser(root, 1);
    Phaser child2 = new Phaser(root, 0);
    assertEquals(0, child2.register());
    assertState(root, 0, 2, 2);
    assertState(child1, 0, 1, 1);
    assertState(child2, 0, 1, 1);
    assertEquals(0, child2.arriveAndDeregister());
    assertState(root, 0, 1, 1);
    assertState(child1, 0, 1, 1);
    assertState(child2, 0, 0, 0);
    assertEquals(0, child2.register());
    assertEquals(0, child2.arriveAndDeregister());
    assertState(root, 0, 1, 1);
    assertState(child1, 0, 1, 1);
    assertState(child2, 0, 0, 0);
    assertEquals(0, child1.arriveAndDeregister());
    assertTerminated(root, 1);
    assertTerminated(child1, 1);
    assertTerminated(child2, 1);
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 38 with Phaser

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

the class PhaserTest method testAwaitAdvanceTieredPhaser.

/**
 * awaitAdvance returns the current phase in child phasers
 */
public void testAwaitAdvanceTieredPhaser() throws Exception {
    final Phaser parent = new Phaser();
    final List<Phaser> zeroPartyChildren = new ArrayList<>(3);
    final List<Phaser> onePartyChildren = new ArrayList<>(3);
    for (int i = 0; i < 3; i++) {
        zeroPartyChildren.add(new Phaser(parent, 0));
        onePartyChildren.add(new Phaser(parent, 1));
    }
    final List<Phaser> phasers = new ArrayList<>();
    phasers.addAll(zeroPartyChildren);
    phasers.addAll(onePartyChildren);
    phasers.add(parent);
    for (Phaser phaser : phasers) {
        assertEquals(-42, phaser.awaitAdvance(-42));
        assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
        assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
    }
    for (Phaser child : onePartyChildren) assertEquals(0, child.arrive());
    for (Phaser phaser : phasers) {
        assertEquals(-42, phaser.awaitAdvance(-42));
        assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
        assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
        assertEquals(1, phaser.awaitAdvance(0));
        assertEquals(1, phaser.awaitAdvanceInterruptibly(0));
        assertEquals(1, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
    }
    for (Phaser child : onePartyChildren) assertEquals(1, child.arrive());
    for (Phaser phaser : phasers) {
        assertEquals(-42, phaser.awaitAdvance(-42));
        assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
        assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
        assertEquals(2, phaser.awaitAdvance(0));
        assertEquals(2, phaser.awaitAdvanceInterruptibly(0));
        assertEquals(2, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
        assertEquals(2, phaser.awaitAdvance(1));
        assertEquals(2, phaser.awaitAdvanceInterruptibly(1));
        assertEquals(2, phaser.awaitAdvanceInterruptibly(1, MEDIUM_DELAY_MS, MILLISECONDS));
    }
}
Also used : ArrayList(java.util.ArrayList) Phaser(java8.util.concurrent.Phaser)

Example 39 with Phaser

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

the class PhaserTest method testConstructor3.

/**
 * The parent provided to the constructor should be returned from
 * a later call to getParent
 */
public void testConstructor3() {
    Phaser parent = new Phaser();
    assertSame(parent, new Phaser(parent).getParent());
    assertNull(new Phaser(null).getParent());
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 40 with Phaser

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

the class PhaserTest method testConstructorDefaultValues.

/**
 * Empty constructor builds a new Phaser with no parent, no registered
 * parties and initial phase number of 0
 */
public void testConstructorDefaultValues() {
    Phaser phaser = new Phaser();
    assertNull(phaser.getParent());
    assertEquals(0, phaser.getRegisteredParties());
    assertEquals(0, phaser.getArrivedParties());
    assertEquals(0, phaser.getUnarrivedParties());
    assertEquals(0, phaser.getPhase());
}
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