Search in sources :

Example 41 with Phaser

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

the class PhaserTest method testRegister1.

/**
 * register() will increment the number of unarrived parties by
 * one and not affect its arrived parties
 */
public void testRegister1() {
    Phaser phaser = new Phaser();
    assertState(phaser, 0, 0, 0);
    assertEquals(0, phaser.register());
    assertState(phaser, 0, 1, 1);
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 42 with Phaser

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

the class PhaserTest method testArriveAndDeregister6.

/**
 * arriveAndDeregister returns the phase in which it leaves the
 * phaser in after deregistration
 */
public void testArriveAndDeregister6() {
    final Phaser phaser = new Phaser(2);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() {
            assertEquals(0, phaser.arrive());
        }
    });
    assertEquals(1, phaser.arriveAndAwaitAdvance());
    assertState(phaser, 1, 2, 2);
    assertEquals(1, phaser.arriveAndDeregister());
    assertState(phaser, 1, 1, 1);
    assertEquals(1, phaser.arriveAndDeregister());
    assertTerminated(phaser, 2);
    awaitTermination(t);
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 43 with Phaser

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

the class PhaserTest method testArriveAndAwaitAdvance3.

/**
 * arriveAndAwaitAdvance waits for all threads to arrive, the
 * number of arrived parties is the same number that is accounted
 * for when the main thread awaitsAdvance
 */
public void testArriveAndAwaitAdvance3() {
    final Phaser phaser = new Phaser(1);
    final int THREADS = 3;
    final CountDownLatch pleaseArrive = new CountDownLatch(THREADS);
    final List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < THREADS; i++) threads.add(newStartedThread(new CheckedRunnable() {

        public void realRun() {
            assertEquals(0, phaser.register());
            pleaseArrive.countDown();
            assertEquals(1, phaser.arriveAndAwaitAdvance());
        }
    }));
    await(pleaseArrive);
    long startTime = System.nanoTime();
    while (phaser.getArrivedParties() < THREADS) Thread.yield();
    assertEquals(THREADS, phaser.getArrivedParties());
    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    for (Thread thread : threads) assertThreadBlocks(thread, Thread.State.WAITING);
    for (Thread thread : threads) assertTrue(thread.isAlive());
    assertState(phaser, 0, THREADS + 1, 1);
    phaser.arriveAndAwaitAdvance();
    for (Thread thread : threads) awaitTermination(thread);
    assertState(phaser, 1, THREADS + 1, THREADS + 1);
}
Also used : ArrayList(java.util.ArrayList) Phaser(java8.util.concurrent.Phaser) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 44 with Phaser

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

the class PhaserTest method testAwaitAdvanceAfterInterrupt.

/**
 * awaitAdvance continues waiting if interrupted before waiting
 */
public void testAwaitAdvanceAfterInterrupt() {
    final Phaser phaser = new Phaser();
    assertEquals(0, phaser.register());
    final CountDownLatch pleaseArrive = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() {
            Thread.currentThread().interrupt();
            assertEquals(0, phaser.register());
            assertEquals(0, phaser.arrive());
            pleaseArrive.countDown();
            assertTrue(Thread.currentThread().isInterrupted());
            assertEquals(1, phaser.awaitAdvance(0));
            assertTrue(Thread.interrupted());
        }
    });
    await(pleaseArrive);
    assertThreadBlocks(t, Thread.State.WAITING);
    assertEquals(0, phaser.arrive());
    awaitTermination(t);
    Thread.currentThread().interrupt();
    assertEquals(1, phaser.awaitAdvance(0));
    assertTrue(Thread.interrupted());
}
Also used : Phaser(java8.util.concurrent.Phaser) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 45 with Phaser

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

the class PhaserTest method testArriveAndDeregister.

/**
 * arriveAndDeregister does not wait for others to arrive at barrier
 */
public void testArriveAndDeregister() {
    final Phaser phaser = new Phaser(1);
    for (int i = 0; i < 10; i++) {
        assertState(phaser, 0, 1, 1);
        assertEquals(0, phaser.register());
        assertState(phaser, 0, 2, 2);
        assertEquals(0, phaser.arriveAndDeregister());
        assertState(phaser, 0, 1, 1);
    }
    assertEquals(0, phaser.arriveAndDeregister());
    assertTerminated(phaser, 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