Search in sources :

Example 31 with Phaser

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

the class PhaserTest method testConstructorNegativeParties2.

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

Example 32 with Phaser

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

the class PhaserTest method testAwaitAdvance4.

/**
 * awaitAdvance atomically waits for all parties within the same phase to
 * complete before continuing
 */
public void testAwaitAdvance4() {
    final Phaser phaser = new Phaser(4);
    final AtomicInteger count = new AtomicInteger(0);
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < 4; i++) threads.add(newStartedThread(new CheckedRunnable() {

        public void realRun() {
            for (int k = 0; k < 3; k++) {
                assertEquals(2 * k + 1, phaser.arriveAndAwaitAdvance());
                count.incrementAndGet();
                assertEquals(2 * k + 1, phaser.arrive());
                assertEquals(2 * k + 2, phaser.awaitAdvance(2 * k + 1));
                assertEquals(4 * (k + 1), count.get());
            }
        }
    }));
    for (Thread thread : threads) awaitTermination(thread);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Phaser(java8.util.concurrent.Phaser)

Example 33 with Phaser

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

the class PhaserTest method testAwaitAdvance1.

/**
 * awaitAdvance succeeds upon advance
 */
public void testAwaitAdvance1() {
    final Phaser phaser = new Phaser(1);
    assertEquals(0, phaser.arrive());
    assertEquals(1, phaser.awaitAdvance(0));
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 34 with Phaser

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

the class PhaserTest method testRegister2.

/**
 * Registering more than 65536 parties causes IllegalStateException
 */
public void testRegister2() {
    Phaser phaser = new Phaser(0);
    assertState(phaser, 0, 0, 0);
    assertEquals(0, phaser.bulkRegister(maxParties - 10));
    assertState(phaser, 0, maxParties - 10, maxParties - 10);
    for (int i = 0; i < 10; i++) {
        assertState(phaser, 0, maxParties - 10 + i, maxParties - 10 + i);
        assertEquals(0, phaser.register());
    }
    assertState(phaser, 0, maxParties, maxParties);
    try {
        phaser.register();
        shouldThrow();
    } catch (IllegalStateException success) {
    }
    try {
        phaser.bulkRegister(Integer.MAX_VALUE);
        shouldThrow();
    } catch (IllegalStateException success) {
    }
    assertEquals(0, phaser.bulkRegister(0));
    assertState(phaser, 0, maxParties, maxParties);
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 35 with Phaser

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

the class PhaserTest method testArriveAndDeregister2.

/**
 * arriveAndDeregister reduces the number of arrived parties
 */
public void testArriveAndDeregister2() {
    final Phaser phaser = new Phaser(1);
    assertEquals(0, phaser.register());
    assertEquals(0, phaser.arrive());
    assertState(phaser, 0, 2, 1);
    assertEquals(0, phaser.arriveAndDeregister());
    assertState(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