Search in sources :

Example 46 with Phaser

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

the class PhaserTest method testArriveAndDeregister1.

/**
 * arriveAndDeregister() throws IllegalStateException if number of
 * registered or unarrived parties would become negative
 */
public void testArriveAndDeregister1() {
    Phaser phaser = new Phaser();
    try {
        phaser.arriveAndDeregister();
        shouldThrow();
    } catch (IllegalStateException success) {
    }
}
Also used : Phaser(java8.util.concurrent.Phaser)

Example 47 with Phaser

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

the class PhaserTest method testArriveAndAwaitAdvanceBeforeInterrupt.

/**
 * arriveAndAwaitAdvance continues waiting if interrupted while waiting
 */
public void testArriveAndAwaitAdvanceBeforeInterrupt() {
    final Phaser phaser = new Phaser();
    assertEquals(0, phaser.register());
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

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

Example 48 with Phaser

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

the class PhaserTest method testAwaitAdvanceInterruptibly_interruptible.

/**
 * awaitAdvanceInterruptibly blocks interruptibly
 */
public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException {
    final Phaser phaser = new Phaser(1);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
    Thread t1 = newStartedThread(new CheckedRunnable() {

        public void realRun() {
            Thread.currentThread().interrupt();
            try {
                phaser.awaitAdvanceInterruptibly(0);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
            pleaseInterrupt.countDown();
            try {
                phaser.awaitAdvanceInterruptibly(0);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
        }
    });
    Thread t2 = newStartedThread(new CheckedRunnable() {

        public void realRun() throws TimeoutException {
            Thread.currentThread().interrupt();
            try {
                phaser.awaitAdvanceInterruptibly(0, 2 * LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
            pleaseInterrupt.countDown();
            try {
                phaser.awaitAdvanceInterruptibly(0, 2 * LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
        }
    });
    await(pleaseInterrupt);
    assertState(phaser, 0, 1, 1);
    assertThreadBlocks(t1, Thread.State.WAITING);
    assertThreadBlocks(t2, Thread.State.TIMED_WAITING);
    t1.interrupt();
    t2.interrupt();
    awaitTermination(t1);
    awaitTermination(t2);
    assertState(phaser, 0, 1, 1);
    assertEquals(0, phaser.arrive());
    assertState(phaser, 1, 1, 1);
}
Also used : Phaser(java8.util.concurrent.Phaser) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException)

Example 49 with Phaser

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

the class PhaserTest method testAwaitAdvanceBeforeInterrupt.

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

        public void realRun() {
            assertEquals(0, phaser.register());
            assertEquals(0, phaser.arrive());
            assertFalse(Thread.currentThread().isInterrupted());
            pleaseArrive.countDown();
            assertEquals(1, phaser.awaitAdvance(0));
            assertTrue(Thread.interrupted());
        }
    });
    await(pleaseArrive);
    assertThreadBlocks(t, Thread.State.WAITING);
    t.interrupt();
    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 50 with Phaser

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

the class PhaserTest method testArriveAndAwaitAdvanceAfterInterrupt.

/**
 * arriveAndAwaitAdvance continues waiting if interrupted before waiting
 */
public void testArriveAndAwaitAdvanceAfterInterrupt() {
    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());
            pleaseArrive.countDown();
            assertTrue(Thread.currentThread().isInterrupted());
            assertEquals(1, phaser.arriveAndAwaitAdvance());
            assertTrue(Thread.interrupted());
        }
    });
    await(pleaseArrive);
    assertThreadBlocks(t, Thread.State.WAITING);
    Thread.currentThread().interrupt();
    assertEquals(1, phaser.arriveAndAwaitAdvance());
    assertTrue(Thread.interrupted());
    awaitTermination(t);
}
Also used : 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