Search in sources :

Example 26 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testTransfer2.

/**
 * 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);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue) Callable(java.util.concurrent.Callable)

Example 27 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testToArray.

/**
 * toArray() contains all elements in FIFO order
 */
public void testToArray() {
    LinkedTransferQueue q = populatedQueue(SIZE);
    Object[] o = q.toArray();
    for (int i = 0; i < o.length; i++) {
        assertSame(o[i], q.poll());
    }
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 28 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testTryTransfer7.

/**
 * 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);
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 29 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testTryTransfer5.

/**
 * tryTransfer blocks interruptibly if no takers
 */
public void testTryTransfer5() throws InterruptedException {
    final LinkedTransferQueue q = new LinkedTransferQueue();
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    assertTrue(q.isEmpty());
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            long startTime = System.nanoTime();
            Thread.currentThread().interrupt();
            try {
                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
            pleaseInterrupt.countDown();
            try {
                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
        }
    });
    await(pleaseInterrupt);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
    checkEmpty(q);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 30 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testTryTransfer2.

/**
 * tryTransfer returns false and does not enqueue if there are no
 * consumers waiting to poll or take.
 */
public void testTryTransfer2() throws InterruptedException {
    final LinkedTransferQueue q = new LinkedTransferQueue();
    assertFalse(q.tryTransfer(new Object()));
    assertFalse(q.hasWaitingConsumer());
    checkEmpty(q);
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Aggregations

LinkedTransferQueue (java.util.concurrent.LinkedTransferQueue)66 ArrayList (java.util.ArrayList)10 Iterator (java.util.Iterator)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 List (java.util.List)4 ExecutorService (java.util.concurrent.ExecutorService)4 Callable (java.util.concurrent.Callable)2 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 MappedByteBuffer (java.nio.MappedByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 AbstractList (java.util.AbstractList)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Queue (java.util.Queue)1 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 TransferQueue (java.util.concurrent.TransferQueue)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1