本文整理汇总了Java中java.util.concurrent.RecursiveTask.fork方法的典型用法代码示例。如果您正苦于以下问题:Java RecursiveTask.fork方法的具体用法?Java RecursiveTask.fork怎么用?Java RecursiveTask.fork使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.RecursiveTask
的用法示例。
在下文中一共展示了RecursiveTask.fork方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}