java.util.concurrent.ForkJoinTask Java Examples
The following examples show how to use
java.util.concurrent.ForkJoinTask.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CountedCompleterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * join of a forked task throws exception when task cancelled */ public void testCancelledForkJoin() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() { CCF f = new LCCF(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); try { f.join(); shouldThrow(); } catch (CancellationException success) { checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); }
Example #2
Source File: CountedCompleterTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * peekNextLocalTask returns most recent unexecuted task. */ public void testPeekNextLocalTask() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() { CCF g = new LCCF(9); assertSame(g, g.fork()); CCF f = new LCCF(8); assertSame(f, f.fork()); assertSame(f, peekNextLocalTask()); assertNull(f.join()); checkCompletedNormally(f); helpQuiesce(); checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); }
Example #3
Source File: ForkJoinTask8Test.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
void checkNotDone(ForkJoinTask a) { assertFalse(a.isDone()); assertFalse(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); assertFalse(a.isCancelled()); assertNull(a.getException()); assertNull(a.getRawResult()); if (a instanceof BinaryAsyncAction) assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == INITIAL_STATE); try { a.get(0L, SECONDS); shouldThrow(); } catch (TimeoutException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } }
Example #4
Source File: CountedCompleterTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * get of a forked task throws exception when task cancelled */ public void testCancelledForkGet() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() throws Exception { CCF f = new LCCF(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); try { f.get(); shouldThrow(); } catch (CancellationException success) { checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); }
Example #5
Source File: ForkJoinTask8Test.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public void testAbnormalInvokeAll3(ForkJoinPool pool) { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); AsyncFib h = new AsyncFib(7); ForkJoinTask[] tasks = { f, g, h }; shuffle(tasks); try { invokeAll(tasks[0], tasks[1], tasks[2]); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); } }}; testInvokeOnPool(pool, a); }
Example #6
Source File: ForkJoinPool8Test.java From j2objc with Apache License 2.0 | 6 votes |
/** * invokeAll(tasks) with > 2 argument throws exception if any task does */ public void testAbnormalInvokeAll3CC() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() { CCF f = new LCCF(null, 8); FailingCCF g = new LFCCF(null, 9); CCF h = new LCCF(null, 7); try { invokeAll(f, g, h); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); } }}; checkInvoke(a); }
Example #7
Source File: ForkJoinTaskTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollectionSingleton() { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { FailingAsyncFib f = new FailingAsyncFib(8); AsyncFib g = new AsyncFib(9); AsyncFib h = new AsyncFib(7); ForkJoinTask[] tasks = { f, g, h }; shuffle(tasks); try { invokeAll(Arrays.asList(tasks)); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(f, success); } }}; testInvokeOnPool(singletonPool(), a); }
Example #8
Source File: CountedCompleterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollection() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() { FailingCCF f = new LFCCF(8); CCF g = new LCCF(9); CCF h = new LCCF(7); HashSet set = new HashSet(); set.add(f); set.add(g); set.add(h); try { invokeAll(set); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(f, success); } }}; testInvokeOnPool(mainPool(), a); }
Example #9
Source File: CountedCompleterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * invokeAll(tasks) with > 2 argument invokes tasks */ public void testInvokeAll3() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() { CCF f = new LCCF(8); CCF g = new LCCF(9); CCF h = new LCCF(7); invokeAll(f, g, h); assertEquals(21, f.number); assertEquals(34, g.number); assertEquals(13, h.number); checkCompletedNormally(f); checkCompletedNormally(g); checkCompletedNormally(h); }}; testInvokeOnPool(mainPool(), a); }
Example #10
Source File: CountedCompleterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * timed get of a forked task throws exception when task cancelled */ public void testCancelledForkTimedGet() throws Exception { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() throws Exception { CCF f = new LCCF(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); try { f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (CancellationException success) { checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); }
Example #11
Source File: CountedCompleterTest.java From j2objc with Apache License 2.0 | 6 votes |
private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) { try (PoolCleaner cleaner = cleaner(pool)) { assertFalse(a.isDone()); assertFalse(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); assertFalse(a.isCancelled()); assertNull(a.getException()); assertNull(a.getRawResult()); assertNull(pool.invoke(a)); assertTrue(a.isDone()); assertTrue(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); assertFalse(a.isCancelled()); assertNull(a.getException()); assertNull(a.getRawResult()); } }
Example #12
Source File: MoreFunctions.java From more-lambdas-java with Artistic License 2.0 | 6 votes |
/** * mainly use for {@link Stream#parallel()} with specific thread pool * see https://stackoverflow.com/questions/21163108/custom-thread-pool-in-java-8-parallel-stream */ public static <R, X extends Throwable> R supplyParallel(ForkJoinPool pool, ThrowableSupplier<R, X> func) throws X { checkNotNull(pool); Throwable[] throwable = { null }; ForkJoinTask<R> task = pool.submit(() -> { try { return func.get(); } catch (Throwable e) { throwable[0] = e; return null; } }); R r; try { r = task.get(); } catch (ExecutionException | InterruptedException impossible) { throw new AssertionError(impossible); } if (throwable[0] != null) { //noinspection unchecked throw (X) throwable[0]; } return r; }
Example #13
Source File: ForkJoinPool8Test.java From j2objc with Apache License 2.0 | 6 votes |
/** * timed get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkTimedGetCC() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() throws Exception { FailingCCF f = new LFCCF(null, 8); assertSame(f, f.fork()); try { f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { Throwable cause = success.getCause(); assertTrue(cause instanceof FJException); checkCompletedAbnormally(f, cause); } }}; checkInvoke(a); }
Example #14
Source File: CountedCompleterTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * invokeAll(tasks) with > 2 argument invokes tasks */ public void testInvokeAll3() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() { CCF f = new LCCF(8); CCF g = new LCCF(9); CCF h = new LCCF(7); invokeAll(f, g, h); assertEquals(21, f.number); assertEquals(34, g.number); assertEquals(13, h.number); checkCompletedNormally(f); checkCompletedNormally(g); checkCompletedNormally(h); }}; testInvokeOnPool(mainPool(), a); }
Example #15
Source File: CountedCompleterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * get of a forked task throws exception when task cancelled */ public void testCancelledForkGet() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() throws Exception { CCF f = new LCCF(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); try { f.get(); shouldThrow(); } catch (CancellationException success) { checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); }
Example #16
Source File: CountedCompleterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * timed get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkTimedGetSingleton() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() throws Exception { FailingCCF f = new LFCCF(8); assertSame(f, f.fork()); try { f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { Throwable cause = success.getCause(); assertTrue(cause instanceof FJException); checkCompletedAbnormally(f, cause); } }}; testInvokeOnPool(singletonPool(), a); }
Example #17
Source File: ForkJoinPool8Test.java From j2objc with Apache License 2.0 | 6 votes |
/** * timed get of a forked task throws exception when task cancelled */ public void testCancelledForkTimedGetCC() throws Exception { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() throws Exception { CCF f = new LCCF(null, 8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); try { f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (CancellationException success) { checkCancelled(f); } }}; checkInvoke(a); }
Example #18
Source File: ArrayDoubler.java From openjdk-systemtest with Apache License 2.0 | 6 votes |
protected void compute() { if(endValue - startValue > MAX_ELEMENTS_TO_PROCESS) // If there are too many elements to process in one operation... { if(ForkJoinTask.inForkJoinPool()) // ... and if we are in a ForkJoinPool ... { int halfWay = (endValue + startValue) / 2; invokeAll(new ArrayDoubler(array, startValue, halfWay), new ArrayDoubler(array, halfWay, endValue)); return; } } for(int i = startValue; i < endValue; i++) // If we aren't in a ForkJoinPool or if there are not a large number of elements to be processed { array[i] = array[i] * 2; } }
Example #19
Source File: CountedCompleterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * invokeAll(tasks) with > 2 argument throws exception if any task does */ public void testAbnormalInvokeAll3() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() { CCF f = new LCCF(8); FailingCCF g = new LFCCF(9); CCF h = new LCCF(7); try { invokeAll(f, g, h); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); } }}; testInvokeOnPool(mainPool(), a); }
Example #20
Source File: ForkJoinPoolTest.java From jdk-source-analysis with Apache License 2.0 | 6 votes |
@Test public void test() { ForkJoinPool pool = new ForkJoinPool(2); String homePath = System.getProperty("user.home"); FileCountTask task = new FileCountTask(homePath); ForkJoinTask<Integer> result = pool.submit(task); try { Integer count = result.get(); System.out.println("file count = " + count); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } pool.shutdown(); while (!pool.isTerminated()) { } System.out.println("All thread finish..."); }
Example #21
Source File: ForkJoinTaskTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * invokeAll(t1, t2) throw exception if any task does */ public void testAbnormalInvokeAll2() { RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { AsyncFib f = new AsyncFib(8); FailingAsyncFib g = new FailingAsyncFib(9); ForkJoinTask[] tasks = { f, g }; shuffle(tasks); try { invokeAll(tasks); shouldThrow(); } catch (FJException success) { checkCompletedAbnormally(g, success); } }}; testInvokeOnPool(mainPool(), a); }
Example #22
Source File: CustomFJPoolTest.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
static int countSplits(ForkJoinPool fjp) throws Exception { // The number of splits will be equivalent to the number of leaf nodes // and will be a power of 2 ForkJoinTask<Integer> fInteger = fjp.submit(() -> { Spliterator<Integer> s = IntStream.range(0, 1024).boxed().parallel().spliterator(); SplitCountingSpliterator<Integer> cs = new SplitCountingSpliterator<>(s); StreamSupport.stream(cs, true).forEach(e -> {}); return cs.splits(); }); return fInteger.get(); }
Example #23
Source File: ForkJoinTaskTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
void checkNotDone(ForkJoinTask a) { assertFalse(a.isDone()); assertFalse(a.isCompletedNormally()); assertFalse(a.isCompletedAbnormally()); assertFalse(a.isCancelled()); assertNull(a.getException()); assertNull(a.getRawResult()); try { a.get(0L, SECONDS); shouldThrow(); } catch (TimeoutException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } }
Example #24
Source File: CountedCompleterTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * quietlyJoin of a forked task returns when task completes abnormally */ public void testAbnormalForkQuietlyJoinSingleton() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() { FailingCCF f = new LFCCF(8); assertSame(f, f.fork()); f.quietlyJoin(); assertTrue(f.getException() instanceof FJException); checkCompletedAbnormally(f, f.getException()); }}; testInvokeOnPool(singletonPool(), a); }
Example #25
Source File: AverageSpeed.java From Java-11-Cookbook-Second-Edition with MIT License | 5 votes |
protected Double compute() { double max = speedLimitByLane[laneNumber - 1]; double min = laneNumber == 1 ? 0.0 : speedLimitByLane[laneNumber - 2]; if (trafficUnitsNumber < threshold) { double speed = FactoryTraffic.getTrafficUnitStream(dateLocation, trafficUnitsNumber) .map(TrafficUnitWrapper::new) .map(tuw -> tuw.setSpeedModel(speedModel)) .map(tuw -> calcSpeed(tuw.getVehicle(), tuw.getTraction(), timeSec)) .mapToDouble(sp -> sp) .filter(sp -> sp > min && sp <= max) .average() .getAsDouble(); return (double) Math.round(speed); } else{ int tun = trafficUnitsNumber / 2; AverageSpeed as1 = new AverageSpeed(tun, timeSec, dateLocation, speedLimitByLane, laneNumber, threshold); AverageSpeed as2 = new AverageSpeed(tun, timeSec, dateLocation, speedLimitByLane, laneNumber, threshold); return ForkJoinTask.invokeAll(List.of(as1, as2)) .stream() .mapToDouble(ForkJoinTask::join) .map(Math::round) .average() .getAsDouble(); } }
Example #26
Source File: CountedCompleterTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * timed get of a forked task returns when task completes */ public void testForkTimedGet() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() throws Exception { CCF f = new LCCF(8); assertSame(f, f.fork()); assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); assertEquals(21, f.number); checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); }
Example #27
Source File: CountedCompleterTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * setRawResult(null) succeeds */ public void testSetRawResult() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() { setRawResult(null); assertNull(getRawResult()); }}; assertNull(a.invoke()); }
Example #28
Source File: AverageSpeed.java From Java-9-Cookbook with MIT License | 5 votes |
private static double doInvokeAll(AverageSpeed as1, AverageSpeed as2){ return ForkJoinTask.invokeAll(List.of(as1, as2)) .stream() .mapToDouble(ForkJoinTask::join) .map(Math::round) .average() .getAsDouble(); }
Example #29
Source File: CountedCompleterTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * quietlyInvoke task returns when task completes normally. * isCompletedAbnormally and isCancelled return false for normally * completed tasks */ public void testQuietlyInvokeSingleton() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() { CCF f = new LCCF(8); f.quietlyInvoke(); assertEquals(21, f.number); checkCompletedNormally(f); }}; testInvokeOnPool(singletonPool(), a); }
Example #30
Source File: CountedCompleterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * quietlyInvoke task returns when task completes abnormally */ public void testAbnormalQuietlyInvoke() { ForkJoinTask a = new CheckedRecursiveAction() { protected void realCompute() { FailingCCF f = new LFCCF(8); f.quietlyInvoke(); assertTrue(f.getException() instanceof FJException); checkCompletedAbnormally(f, f.getException()); }}; testInvokeOnPool(mainPool(), a); }