Search in sources :

Example 41 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.

the class LinkedTransferQueueTest method testTransfer4.

/**
     * transfer waits until a poll occurs, at which point the polling
     * thread returns the element
     */
public void testTransfer4() throws InterruptedException {
    final LinkedTransferQueue q = new LinkedTransferQueue();
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            q.transfer(four);
            assertFalse(q.contains(four));
            assertSame(three, q.poll());
        }
    });
    while (q.isEmpty()) Thread.yield();
    assertFalse(q.isEmpty());
    assertEquals(1, q.size());
    assertTrue(q.offer(three));
    assertSame(four, q.poll());
    awaitTermination(t);
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 42 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.

the class LinkedTransferQueueTest method testTryTransfer4.

/**
     * If there is a consumer waiting in take, tryTransfer returns
     * true while successfully transfering object.
     */
public void testTryTransfer4() throws InterruptedException {
    final Object hotPotato = new Object();
    final LinkedTransferQueue q = new LinkedTransferQueue();
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() {
            while (!q.hasWaitingConsumer()) Thread.yield();
            assertTrue(q.hasWaitingConsumer());
            checkEmpty(q);
            assertTrue(q.tryTransfer(hotPotato));
        }
    });
    assertSame(q.take(), hotPotato);
    checkEmpty(q);
    awaitTermination(t);
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 43 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.

the class LinkedTransferQueueTest method testTryTransfer3.

/**
     * If there is a consumer waiting in timed poll, tryTransfer
     * returns true while successfully transfering object.
     */
public void testTryTransfer3() throws InterruptedException {
    final Object hotPotato = new Object();
    final LinkedTransferQueue q = new LinkedTransferQueue();
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() {
            while (!q.hasWaitingConsumer()) Thread.yield();
            assertTrue(q.hasWaitingConsumer());
            checkEmpty(q);
            assertTrue(q.tryTransfer(hotPotato));
        }
    });
    long startTime = System.nanoTime();
    assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS));
    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    checkEmpty(q);
    awaitTermination(t);
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 44 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.

the class LinkedTransferQueueTest method testTryTransfer8.

/**
     * tryTransfer attempts to enqueue into the queue and fails
     * returning false not enqueueing and the successive poll is null
     */
public void testTryTransfer8() throws InterruptedException {
    final LinkedTransferQueue q = new LinkedTransferQueue();
    assertTrue(q.offer(four));
    assertEquals(1, q.size());
    long startTime = System.nanoTime();
    assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
    assertEquals(1, q.size());
    assertSame(four, q.poll());
    assertNull(q.poll());
    checkEmpty(q);
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 45 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.

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