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

The following examples show how to use java.util.concurrent.LinkedTransferQueue#size() . 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 6 votes vote down vote up
/**
 * transfer waits until a poll occurs. The transfered element
 * is returned by the associated poll.
 */
public void testTransfer2() throws InterruptedException {
    final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
    final CountDownLatch threadStarted = new CountDownLatch(1);

    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            threadStarted.countDown();
            q.transfer(five);
            checkEmpty(q);
        }});

    threadStarted.await();
    Callable<Boolean> oneElement
        = new Callable<Boolean>() { public Boolean call() {
            return !q.isEmpty() && q.size() == 1; }};
    waitForThreadToEnterWaitState(t, oneElement);

    assertSame(five, q.poll());
    checkEmpty(q);
    awaitTermination(t);
}
 
Example 2
Source File: LinkedTransferQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * tryTransfer waits for any elements previously in to be removed
 * before transfering to a poll or take
 */
public void testTryTransfer7() throws InterruptedException {
    final LinkedTransferQueue q = new LinkedTransferQueue();
    assertTrue(q.offer(four));

    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            long startTime = System.nanoTime();
            assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS));
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
            checkEmpty(q);
        }});

    while (q.size() != 2)
        Thread.yield();
    assertEquals(2, q.size());
    assertSame(four, q.poll());
    assertSame(five, q.poll());
    checkEmpty(q);
    awaitTermination(t);
}
 
Example 3
Source File: LinkedTransferQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * transfer waits until a poll occurs. The transfered element
 * is returned by this associated poll.
 */
public void testTransfer2() throws InterruptedException {
    final LinkedTransferQueue<Integer> q
        = new LinkedTransferQueue<Integer>();
    final CountDownLatch threadStarted = new CountDownLatch(1);

    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            threadStarted.countDown();
            q.transfer(five);
            checkEmpty(q);
        }});

    threadStarted.await();
    Callable<Boolean> oneElement
        = new Callable<Boolean>() { public Boolean call() {
            return !q.isEmpty() && q.size() == 1; }};
    waitForThreadToEnterWaitState(t, oneElement);

    assertSame(five, q.poll());
    checkEmpty(q);
    awaitTermination(t);
}
 
Example 4
Source File: LinkedTransferQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * tryTransfer waits for any elements previously in to be removed
 * before transfering to a poll or take
 */
public void testTryTransfer7() throws InterruptedException {
    final LinkedTransferQueue q = new LinkedTransferQueue();
    assertTrue(q.offer(four));

    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            long startTime = System.nanoTime();
            assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS));
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
            checkEmpty(q);
        }});

    while (q.size() != 2)
        Thread.yield();
    assertEquals(2, q.size());
    assertSame(four, q.poll());
    assertSame(five, q.poll());
    checkEmpty(q);
    awaitTermination(t);
}
 
Example 5
Source File: LinkedTransferQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * transfer waits until a poll occurs, and then transfers in fifo order
 */
public void testTransfer3() throws InterruptedException {
    final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();

    Thread first = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            q.transfer(four);
            assertTrue(!q.contains(four));
            assertEquals(1, q.size());
        }});

    Thread interruptedThread = newStartedThread(
        new CheckedInterruptedRunnable() {
            public void realRun() throws InterruptedException {
                while (q.isEmpty())
                    Thread.yield();
                q.transfer(five);
            }});

    while (q.size() < 2)
        Thread.yield();
    assertEquals(2, q.size());
    assertSame(four, q.poll());
    first.join();
    assertEquals(1, q.size());
    interruptedThread.interrupt();
    interruptedThread.join();
    checkEmpty(q);
}
 
Example 6
Source File: LinkedTransferQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * transfer waits until a poll occurs, and then transfers in fifo order
 */
public void testTransfer3() throws InterruptedException {
    final LinkedTransferQueue<Integer> q
        = new LinkedTransferQueue<Integer>();

    Thread first = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            q.transfer(four);
            assertTrue(!q.contains(four));
            assertEquals(1, q.size());
        }});

    Thread interruptedThread = newStartedThread(
        new CheckedInterruptedRunnable() {
            public void realRun() throws InterruptedException {
                while (q.isEmpty())
                    Thread.yield();
                q.transfer(five);
            }});

    while (q.size() < 2)
        Thread.yield();
    assertEquals(2, q.size());
    assertSame(four, q.poll());
    first.join();
    assertEquals(1, q.size());
    interruptedThread.interrupt();
    interruptedThread.join();
    checkEmpty(q);
}