本文整理汇总了Java中java.util.concurrent.RecursiveTask类的典型用法代码示例。如果您正苦于以下问题:Java RecursiveTask类的具体用法?Java RecursiveTask怎么用?Java RecursiveTask使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RecursiveTask类属于java.util.concurrent包,在下文中一共展示了RecursiveTask类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compute
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
@Override
protected List<WordStatistic> compute() {
if (lines.size() <= countOfLinesThreshold) {
return lines.
stream().
filter(StringUtils::isNotEmpty).
map(this::countWordsInLine).
reduce(Collections.emptyList(), this::mergeWordStatistics);
}
RecursiveTask<List<WordStatistic>> recursiveTask = new WordStatisticsInLinesRecursiveTask(lines.subList(0, countOfLinesThreshold), countOfLinesThreshold, wordSeparator);
recursiveTask.fork();
return mergeWordStatistics(
new WordStatisticsInLinesRecursiveTask(lines.subList(countOfLinesThreshold, lines.size()), countOfLinesThreshold, wordSeparator).compute(),
recursiveTask.join()
);
}
示例2: realMain
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
private static void realMain(String[] args) throws Throwable {
if (debug) {
String pp = System.getProperty(
"java.util.concurrent.ForkJoinPool.common.parallelism");
System.out.println(
"java.util.concurrent.ForkJoinPool.common.parallelism:" + pp);
String tf = System.getProperty(
"java.util.concurrent.ForkJoinPool.common.threadFactory");
System.out.println(
"java.util.concurrent.ForkJoinPool.common.threadFactory:" + tf);
}
long from = 0, to = 50000;
RecursiveTask<Long> task = new SumTask(from, to, Thread.currentThread());
long sum = task.invoke();
System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);
task.fork();
sum = task.join();
System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);
sum = ForkJoinPool.commonPool().invoke(task.fork());
System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);
}
示例3: testAbnormalForkJoin
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* join of a forked task throws exception when task completes abnormally
*/
public void testAbnormalForkJoin() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() {
FailingFibTask f = new FailingFibTask(8);
assertSame(f, f.fork());
try {
Integer r = f.join();
shouldThrow();
} catch (FJException success) {
checkCompletedAbnormally(f, success);
}
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例4: testAbnormalForkGet
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* get of a forked task throws exception when task completes abnormally
*/
public void testAbnormalForkGet() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() throws Exception {
FailingFibTask f = new FailingFibTask(8);
assertSame(f, f.fork());
try {
Integer r = f.get();
shouldThrow();
} catch (ExecutionException success) {
Throwable cause = success.getCause();
assertTrue(cause instanceof FJException);
checkCompletedAbnormally(f, cause);
}
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例5: testAbnormalForkTimedGet
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* timed get of a forked task throws exception when task completes abnormally
*/
public void testAbnormalForkTimedGet() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() throws Exception {
FailingFibTask f = new FailingFibTask(8);
assertSame(f, f.fork());
try {
Integer r = f.get(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (ExecutionException success) {
Throwable cause = success.getCause();
assertTrue(cause instanceof FJException);
checkCompletedAbnormally(f, cause);
}
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例6: testCancelledInvoke
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* invoke task throws exception when task cancelled
*/
public void testCancelledInvoke() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() {
FibTask f = new FibTask(8);
assertTrue(f.cancel(true));
try {
Integer r = f.invoke();
shouldThrow();
} catch (CancellationException success) {
checkCancelled(f);
}
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例7: testCancelledForkJoin
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* join of a forked task throws exception when task cancelled
*/
public void testCancelledForkJoin() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() {
FibTask f = new FibTask(8);
assertTrue(f.cancel(true));
assertSame(f, f.fork());
try {
Integer r = f.join();
shouldThrow();
} catch (CancellationException success) {
checkCancelled(f);
}
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例8: testCancelledForkGet
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* get of a forked task throws exception when task cancelled
*/
public void testCancelledForkGet() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() throws Exception {
FibTask f = new FibTask(8);
assertTrue(f.cancel(true));
assertSame(f, f.fork());
try {
Integer r = f.get();
shouldThrow();
} catch (CancellationException success) {
checkCancelled(f);
}
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例9: testCancelledForkTimedGet
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* timed get of a forked task throws exception when task cancelled
*/
public void testCancelledForkTimedGet() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() throws Exception {
FibTask f = new FibTask(8);
assertTrue(f.cancel(true));
assertSame(f, f.fork());
try {
Integer r = f.get(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (CancellationException success) {
checkCancelled(f);
}
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例10: testReinitialize
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* A reinitialized normally completed task may be re-invoked
*/
public void testReinitialize() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() {
FibTask f = new FibTask(8);
checkNotDone(f);
for (int i = 0; i < 3; i++) {
Integer r = f.invoke();
assertEquals(21, (int) r);
checkCompletedNormally(f, r);
f.reinitialize();
f.publicSetRawResult(null);
checkNotDone(f);
}
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例11: testReinitializeAbnormal
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* A reinitialized abnormally completed task may be re-invoked
*/
public void testReinitializeAbnormal() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() {
FailingFibTask f = new FailingFibTask(8);
checkNotDone(f);
for (int i = 0; i < 3; i++) {
try {
f.invoke();
shouldThrow();
} catch (FJException success) {
checkCompletedAbnormally(f, success);
}
f.reinitialize();
checkNotDone(f);
}
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例12: testCompleteExceptionally
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* invoke task throws exception after invoking completeExceptionally
*/
public void testCompleteExceptionally() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() {
FibTask f = new FibTask(8);
f.completeExceptionally(new FJException());
try {
Integer r = f.invoke();
shouldThrow();
} catch (FJException success) {
checkCompletedAbnormally(f, success);
}
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例13: testInvokeAll3
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* invokeAll(tasks) with > 2 argument invokes tasks
*/
public void testInvokeAll3() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() {
FibTask f = new FibTask(8);
FibTask g = new FibTask(9);
FibTask h = new FibTask(7);
invokeAll(f, g, h);
assertTrue(f.isDone());
assertTrue(g.isDone());
assertTrue(h.isDone());
checkCompletedNormally(f, 21);
checkCompletedNormally(g, 34);
checkCompletedNormally(h, 13);
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例14: testInvokeAllCollection
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* invokeAll(collection) invokes all tasks in the collection
*/
public void testInvokeAllCollection() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() {
FibTask f = new FibTask(8);
FibTask g = new FibTask(9);
FibTask h = new FibTask(7);
HashSet set = new HashSet();
set.add(f);
set.add(g);
set.add(h);
invokeAll(set);
assertTrue(f.isDone());
assertTrue(g.isDone());
assertTrue(h.isDone());
checkCompletedNormally(f, 21);
checkCompletedNormally(g, 34);
checkCompletedNormally(h, 13);
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
示例15: testAbnormalInvokeAll2
import java.util.concurrent.RecursiveTask; //导入依赖的package包/类
/**
* invokeAll(t1, t2) throw exception if any task does
*/
public void testAbnormalInvokeAll2() {
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
public Integer realCompute() {
FibTask f = new FibTask(8);
FailingFibTask g = new FailingFibTask(9);
try {
invokeAll(f, g);
shouldThrow();
} catch (FJException success) {
checkCompletedAbnormally(g, success);
}
return NoResult;
}};
assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}