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);
}
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));
}
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());
}
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);
}
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) {
}
}
Aggregations