Search in sources :

Example 36 with LinkedBlockingQueue

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

the class LinkedBlockingQueueTest method testBlockingPut.

/**
     * put blocks interruptibly if full
     */
public void testBlockingPut() throws InterruptedException {
    final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            for (int i = 0; i < SIZE; ++i) q.put(i);
            assertEquals(SIZE, q.size());
            assertEquals(0, q.remainingCapacity());
            Thread.currentThread().interrupt();
            try {
                q.put(99);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
            pleaseInterrupt.countDown();
            try {
                q.put(99);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
        }
    });
    await(pleaseInterrupt);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
    assertEquals(SIZE, q.size());
    assertEquals(0, q.remainingCapacity());
}
Also used : LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 37 with LinkedBlockingQueue

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

the class LinkedBlockingQueueTest method testClear.

/**
     * clear removes all elements
     */
public void testClear() {
    LinkedBlockingQueue q = populatedQueue(SIZE);
    q.clear();
    assertTrue(q.isEmpty());
    assertEquals(0, q.size());
    assertEquals(SIZE, q.remainingCapacity());
    q.add(one);
    assertFalse(q.isEmpty());
    assertTrue(q.contains(one));
    q.clear();
    assertTrue(q.isEmpty());
}
Also used : LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 38 with LinkedBlockingQueue

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

the class LinkedBlockingQueueTest method testDrainToN.

/**
     * drainTo(c, n) empties first min(n, size) elements of queue into c
     */
public void testDrainToN() {
    LinkedBlockingQueue q = new LinkedBlockingQueue();
    for (int i = 0; i < SIZE + 2; ++i) {
        for (int j = 0; j < SIZE; j++) assertTrue(q.offer(new Integer(j)));
        ArrayList l = new ArrayList();
        q.drainTo(l, i);
        int k = (i < SIZE) ? i : SIZE;
        assertEquals(k, l.size());
        assertEquals(SIZE - k, q.size());
        for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j));
        do {
        } while (q.poll() != null);
    }
}
Also used : ArrayList(java.util.ArrayList) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 39 with LinkedBlockingQueue

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

the class LinkedBlockingQueueTest method testRemoveElementAndAdd.

/**
     * An add following remove(x) succeeds
     */
public void testRemoveElementAndAdd() throws InterruptedException {
    LinkedBlockingQueue q = new LinkedBlockingQueue();
    assertTrue(q.add(new Integer(1)));
    assertTrue(q.add(new Integer(2)));
    assertTrue(q.remove(new Integer(1)));
    assertTrue(q.remove(new Integer(2)));
    assertTrue(q.add(new Integer(3)));
    assertNotNull(q.take());
}
Also used : LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 40 with LinkedBlockingQueue

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

the class LinkedBlockingQueueTest method testPutWithTake.

/**
     * put blocks interruptibly waiting for take when full
     */
public void testPutWithTake() throws InterruptedException {
    final int capacity = 2;
    final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
    final CountDownLatch pleaseTake = new CountDownLatch(1);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            for (int i = 0; i < capacity; i++) q.put(i);
            pleaseTake.countDown();
            q.put(86);
            pleaseInterrupt.countDown();
            try {
                q.put(99);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
        }
    });
    await(pleaseTake);
    assertEquals(0, q.remainingCapacity());
    assertEquals(0, q.take());
    await(pleaseInterrupt);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
    assertEquals(0, q.remainingCapacity());
}
Also used : LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)259 Test (org.junit.Test)91 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)64 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)23 Emitter (io.socket.emitter.Emitter)19 JSONObject (org.json.JSONObject)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 ThreadFactory (java.util.concurrent.ThreadFactory)16 ExecutorService (java.util.concurrent.ExecutorService)14 BlockingQueue (java.util.concurrent.BlockingQueue)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 List (java.util.List)12 URI (java.net.URI)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Intent (android.content.Intent)9 HashMap (java.util.HashMap)9 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)8 Map (java.util.Map)8 UUID (java.util.UUID)8