Search in sources :

Example 91 with SynchronousQueue

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

the class RecursiveActionTest method testJoinIgnoresInterruptsOutsideForkJoinPool.

/**
 * join/quietlyJoin of a forked task when not in ForkJoinPool
 * succeeds in the presence of interrupts
 */
public void testJoinIgnoresInterruptsOutsideForkJoinPool() {
    final SynchronousQueue<FibAction[]> sq = new SynchronousQueue<FibAction[]>();
    RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() throws InterruptedException {
            FibAction[] fibActions = new FibAction[6];
            for (int i = 0; i < fibActions.length; i++) fibActions[i] = new FibAction(8);
            fibActions[1].cancel(false);
            fibActions[2].completeExceptionally(new FJException());
            fibActions[4].cancel(true);
            fibActions[5].completeExceptionally(new FJException());
            for (int i = 0; i < fibActions.length; i++) fibActions[i].fork();
            sq.put(fibActions);
            helpQuiesce();
        }
    };
    Runnable r = new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            FibAction[] fibActions = sq.take();
            FibAction f;
            final Thread myself = Thread.currentThread();
            // test join() ------------
            f = fibActions[0];
            assertFalse(ForkJoinTask.inForkJoinPool());
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            assertNull(f.join());
            assertTrue(Thread.interrupted());
            assertEquals(21, f.result);
            checkCompletedNormally(f);
            f = fibActions[1];
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            try {
                f.join();
                shouldThrow();
            } catch (CancellationException success) {
                assertTrue(Thread.interrupted());
                checkCancelled(f);
            }
            f = fibActions[2];
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            try {
                f.join();
                shouldThrow();
            } catch (FJException success) {
                assertTrue(Thread.interrupted());
                checkCompletedAbnormally(f, success);
            }
            // test quietlyJoin() ---------
            f = fibActions[3];
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            f.quietlyJoin();
            assertTrue(Thread.interrupted());
            assertEquals(21, f.result);
            checkCompletedNormally(f);
            f = fibActions[4];
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            f.quietlyJoin();
            assertTrue(Thread.interrupted());
            checkCancelled(f);
            f = fibActions[5];
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            f.quietlyJoin();
            assertTrue(Thread.interrupted());
            assertTrue(f.getException() instanceof FJException);
            checkCompletedAbnormally(f, f.getException());
        }
    };
    Thread t;
    t = newStartedThread(r);
    testInvokeOnPool(mainPool(), a);
    awaitTermination(t);
    a.reinitialize();
    t = newStartedThread(r);
    testInvokeOnPool(singletonPool(), a);
    awaitTermination(t);
}
Also used : CancellationException(java.util.concurrent.CancellationException) SynchronousQueue(java.util.concurrent.SynchronousQueue) RecursiveAction(java.util.concurrent.RecursiveAction) ForkJoinWorkerThread(java.util.concurrent.ForkJoinWorkerThread)

Example 92 with SynchronousQueue

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

the class SynchronousQueueTest method testEmptyFull.

public void testEmptyFull(boolean fair) {
    final SynchronousQueue q = new SynchronousQueue(fair);
    assertTrue(q.isEmpty());
    assertEquals(0, q.size());
    assertEquals(0, q.remainingCapacity());
    assertFalse(q.offer(zero));
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue)

Example 93 with SynchronousQueue

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

the class SynchronousQueueTest method testBlockingPut.

public void testBlockingPut(boolean fair) {
    final SynchronousQueue q = new SynchronousQueue(fair);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            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(0, q.remainingCapacity());
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 94 with SynchronousQueue

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

the class SynchronousQueueTest method testDrainToN.

/**
 * drainTo(c, n) empties up to n elements of queue into c
 */
public void testDrainToN() throws InterruptedException {
    final SynchronousQueue q = new SynchronousQueue();
    Thread t1 = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            q.put(one);
        }
    });
    Thread t2 = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            q.put(two);
        }
    });
    ArrayList l = new ArrayList();
    int drained;
    while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
    assertEquals(1, drained);
    assertEquals(1, l.size());
    while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
    assertEquals(1, drained);
    assertEquals(2, l.size());
    assertTrue(l.contains(one));
    assertTrue(l.contains(two));
    awaitTermination(t1);
    awaitTermination(t2);
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue) ArrayList(java.util.ArrayList)

Example 95 with SynchronousQueue

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

the class SynchronousQueueTest method testAddAll_self.

public void testAddAll_self(boolean fair) {
    SynchronousQueue q = new SynchronousQueue(fair);
    try {
        q.addAll(q);
        shouldThrow();
    } catch (IllegalArgumentException success) {
    }
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue)

Aggregations

SynchronousQueue (java.util.concurrent.SynchronousQueue)120 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)64 ExecutorService (java.util.concurrent.ExecutorService)21 ThreadFactory (java.util.concurrent.ThreadFactory)15 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)13 ArrayList (java.util.ArrayList)12 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)10 IOException (java.io.IOException)9 Test (org.junit.Test)9 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 BlockingQueue (java.util.concurrent.BlockingQueue)7 XMPPException (org.jivesoftware.smack.XMPPException)7 Future (java.util.concurrent.Future)6 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 XMPPConnection (org.jivesoftware.smack.XMPPConnection)5 List (java.util.List)4