use of java8.util.concurrent.ForkJoinPool in project streamsupport by stefan-zobel.
the class SubmissionTest method main.
public static void main(String[] args) {
final ForkJoinPool e = new ForkJoinPool(1);
final AtomicBoolean b = new AtomicBoolean();
final Runnable setFalse = () -> b.set(false);
for (int i = 0; i < 30_000; i++) {
b.set(true);
e.execute(setFalse);
long startTime = System.nanoTime();
while (b.get()) {
if (millisElapsedSince(startTime) >= LONG_DELAY_MS) {
throw new RuntimeException("Submitted task failed to execute");
}
Thread.yield();
}
}
}
use of java8.util.concurrent.ForkJoinPool in project streamsupport by stefan-zobel.
the class CustomFJPoolTest method testCustomPools.
public void testCustomPools() throws Exception {
int splitsForP1 = countSplits(new ForkJoinPool(1));
int splitsForP2 = countSplits(new ForkJoinPool(2));
assertEquals(splitsForP2, splitsForP1 * 2);
int commonParallelism = ForkJoinPool.getCommonPoolParallelism();
if (commonParallelism > 1 && commonParallelism < 128) {
int splitsForPHalfC = countSplits(new ForkJoinPool(ForkJoinPool.getCommonPoolParallelism() / 2));
int splitsForPC = countSplits(ForkJoinPool.commonPool());
assertTrue(splitsForPHalfC < splitsForPC);
assertEquals(splitsForPC / splitsForPHalfC, nearestPowerOfTwo(commonParallelism) / nearestPowerOfTwo(commonParallelism / 2));
}
}
use of java8.util.concurrent.ForkJoinPool in project streamsupport by stefan-zobel.
the class ForkJoinPool8Test method testAwaitQuiescence2.
/**
* awaitQuiescence returns when pool isQuiescent() or the indicated
* timeout elapsed
*/
public void testAwaitQuiescence2() throws Exception {
/**
* """It is possible to disable or limit the use of threads in the
* common pool by setting the parallelism property to zero. However
* doing so may cause unjoined tasks to never be executed."""
*/
if ("0".equals(System.getProperty("java.util.concurrent.ForkJoinPool.common.parallelism")))
return;
final ForkJoinPool p = new ForkJoinPool();
PoolCleaner cleaner = null;
try {
cleaner = cleaner(p);
assertTrue(p.isQuiescent());
final long startTime = System.nanoTime();
@SuppressWarnings("serial") ForkJoinTask<?> a = new CheckedRecursiveAction() {
protected void realCompute() {
FibAction f = new FibAction(8);
assertSame(f, f.fork());
while (!f.isDone() && millisElapsedSince(startTime) < LONG_DELAY_MS) {
assertFalse(p.getAsyncMode());
assertFalse(p.isShutdown());
assertFalse(p.isTerminating());
assertFalse(p.isTerminated());
Thread.yield();
}
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
assertEquals(0, ForkJoinTask.getQueuedTaskCount());
assertEquals(21, f.result);
}
};
p.execute(a);
assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
assertTrue(p.isQuiescent());
assertTrue(a.isDone());
assertEquals(0, p.getQueuedTaskCount());
assertFalse(p.getAsyncMode());
assertEquals(0, p.getQueuedSubmissionCount());
assertFalse(p.hasQueuedSubmissions());
while (p.getActiveThreadCount() != 0 && millisElapsedSince(startTime) < LONG_DELAY_MS) Thread.yield();
assertFalse(p.isShutdown());
assertFalse(p.isTerminating());
assertFalse(p.isTerminated());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
} finally {
if (cleaner != null) {
cleaner.close();
}
}
}
use of java8.util.concurrent.ForkJoinPool in project streamsupport by stefan-zobel.
the class ForkJoinPool8Test method testAwaitQuiescence1.
/**
* awaitQuiescence by a worker is equivalent in effect to
* ForkJoinTask.helpQuiesce()
*/
public void testAwaitQuiescence1() throws Exception {
final ForkJoinPool p = new ForkJoinPool();
PoolCleaner cleaner = null;
try {
cleaner = cleaner(p);
final long startTime = System.nanoTime();
assertTrue(p.isQuiescent());
@SuppressWarnings("serial") ForkJoinTask<?> a = new CheckedRecursiveAction() {
protected void realCompute() {
FibAction f = new FibAction(8);
assertSame(f, f.fork());
assertSame(p, ForkJoinTask.getPool());
boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
assertTrue(quiescent);
assertFalse(p.isQuiescent());
while (!f.isDone()) {
assertFalse(p.getAsyncMode());
assertFalse(p.isShutdown());
assertFalse(p.isTerminating());
assertFalse(p.isTerminated());
Thread.yield();
}
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
assertFalse(p.isQuiescent());
assertEquals(0, ForkJoinTask.getQueuedTaskCount());
assertEquals(21, f.result);
}
};
p.execute(a);
while (!a.isDone() || !p.isQuiescent()) {
assertFalse(p.getAsyncMode());
assertFalse(p.isShutdown());
assertFalse(p.isTerminating());
assertFalse(p.isTerminated());
Thread.yield();
}
assertEquals(0, p.getQueuedTaskCount());
assertFalse(p.getAsyncMode());
assertEquals(0, p.getQueuedSubmissionCount());
assertFalse(p.hasQueuedSubmissions());
while (p.getActiveThreadCount() != 0 && millisElapsedSince(startTime) < LONG_DELAY_MS) Thread.yield();
assertFalse(p.isShutdown());
assertFalse(p.isTerminating());
assertFalse(p.isTerminated());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
} finally {
if (cleaner != null) {
cleaner.close();
}
}
}
use of java8.util.concurrent.ForkJoinPool in project streamsupport by stefan-zobel.
the class ForkJoinPoolTest method testInvokeAll5.
/**
* invokeAll(c) returns results of all completed tasks in c
*/
public void testInvokeAll5() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
PoolCleaner cleaner = null;
try {
cleaner = cleaner(e);
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
l.add(new StringTask());
List<Future<String>> futures = e.invokeAll(l);
assertEquals(2, futures.size());
for (Future<String> future : futures) assertSame(TEST_STRING, future.get());
} finally {
if (cleaner != null) {
cleaner.close();
}
}
}
Aggregations