Search in sources :

Example 11 with LinkedTransferQueue

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

the class LinkedTransferQueueTest method testRemoveElementAndAdd.

/**
     * An add following remove(x) succeeds
     */
public void testRemoveElementAndAdd() throws InterruptedException {
    LinkedTransferQueue q = new LinkedTransferQueue();
    assertTrue(q.add(one));
    assertTrue(q.add(two));
    assertTrue(q.remove(one));
    assertTrue(q.remove(two));
    assertTrue(q.add(three));
    assertSame(q.take(), three);
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 12 with LinkedTransferQueue

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

the class LinkedTransferQueueTest method testDrainTo.

/**
     * drainTo(c) empties queue into another collection c
     */
public void testDrainTo() {
    LinkedTransferQueue q = populatedQueue(SIZE);
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertEquals(0, q.size());
    assertEquals(SIZE, l.size());
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, l.get(i));
    }
    q.add(zero);
    q.add(one);
    assertFalse(q.isEmpty());
    assertTrue(q.contains(zero));
    assertTrue(q.contains(one));
    l.clear();
    q.drainTo(l);
    assertEquals(0, q.size());
    assertEquals(2, l.size());
    for (int i = 0; i < 2; ++i) {
        assertEquals(i, l.get(i));
    }
}
Also used : ArrayList(java.util.ArrayList) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 13 with LinkedTransferQueue

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

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 14 with LinkedTransferQueue

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

the class LinkedTransferQueueTest method testAddAll3.

/**
     * addAll of a collection with any null elements throws
     * NullPointerException after possibly adding some elements
     */
public void testAddAll3() {
    LinkedTransferQueue q = new LinkedTransferQueue();
    Integer[] ints = new Integer[SIZE];
    for (int i = 0; i < SIZE - 1; ++i) ints[i] = i;
    try {
        q.addAll(Arrays.asList(ints));
        shouldThrow();
    } catch (NullPointerException success) {
    }
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 15 with LinkedTransferQueue

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

the class LinkedTransferQueueTest method testWaitingConsumer.

/**
     * 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();
    waitForThreadToEnterWaitState(t);
    assertEquals(1, q.getWaitingConsumerCount());
    assertTrue(q.hasWaitingConsumer());
    assertTrue(q.offer(one));
    assertEquals(0, q.getWaitingConsumerCount());
    assertFalse(q.hasWaitingConsumer());
    awaitTermination(t);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) 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