Search in sources :

Example 61 with LinkedTransferQueue

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

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

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

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

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

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

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

the class LinkedTransferQueueTest method testAddAll5.

/**
 * Queue contains all elements, in traversal order, of successful addAll
 */
public void testAddAll5() {
    Integer[] empty = new Integer[0];
    Integer[] ints = new Integer[SIZE];
    for (int i = 0; i < SIZE; ++i) {
        ints[i] = i;
    }
    LinkedTransferQueue q = new LinkedTransferQueue();
    assertFalse(q.addAll(Arrays.asList(empty)));
    assertTrue(q.addAll(Arrays.asList(ints)));
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(ints[i], q.poll());
    }
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 65 with LinkedTransferQueue

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

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)

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