Java Code Examples for java.util.concurrent.LinkedTransferQueue#hasWaitingConsumer()

The following examples show how to use java.util.concurrent.LinkedTransferQueue#hasWaitingConsumer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkedTransferQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * timed poll() or take() increments the waiting consumer count;
 * offer(e) decrements the waiting consumer count
 */
public void testWaitingConsumer() throws InterruptedException {
    final LinkedTransferQueue q = new LinkedTransferQueue();
    assertEquals(0, q.getWaitingConsumerCount());
    assertFalse(q.hasWaitingConsumer());
    final CountDownLatch threadStarted = new CountDownLatch(1);

    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            threadStarted.countDown();
            long startTime = System.nanoTime();
            assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
            assertEquals(0, q.getWaitingConsumerCount());
            assertFalse(q.hasWaitingConsumer());
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
        }});

    threadStarted.await();
    Callable<Boolean> oneConsumer
        = new Callable<Boolean>() { public Boolean call() {
            return q.hasWaitingConsumer()
            && q.getWaitingConsumerCount() == 1; }};
    waitForThreadToEnterWaitState(t, oneConsumer);

    assertTrue(q.offer(one));
    assertEquals(0, q.getWaitingConsumerCount());
    assertFalse(q.hasWaitingConsumer());

    awaitTermination(t);
}
 
Example 2
Source File: LinkedTransferQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * timed poll() or take() increments the waiting consumer count;
 * offer(e) decrements the waiting consumer count
 */
public void testWaitingConsumer() throws InterruptedException {
    final LinkedTransferQueue q = new LinkedTransferQueue();
    assertEquals(0, q.getWaitingConsumerCount());
    assertFalse(q.hasWaitingConsumer());
    final CountDownLatch threadStarted = new CountDownLatch(1);

    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            threadStarted.countDown();
            long startTime = System.nanoTime();
            assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
            assertEquals(0, q.getWaitingConsumerCount());
            assertFalse(q.hasWaitingConsumer());
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
        }});

    threadStarted.await();
    Callable<Boolean> oneConsumer
        = new Callable<Boolean>() { public Boolean call() {
            return q.hasWaitingConsumer()
            && q.getWaitingConsumerCount() == 1; }};
    waitForThreadToEnterWaitState(t, oneConsumer);

    assertTrue(q.offer(one));
    assertEquals(0, q.getWaitingConsumerCount());
    assertFalse(q.hasWaitingConsumer());

    awaitTermination(t);
}