- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图理解 ForkJoinPools,所以我制作了以下简单的测试类。不幸的是它没有达到我的预期。谁能指出我哪里出错了?
import java.util.*;
import java.util.concurrent.*;
public class UtilsShowcase {
public static void main(String[] args){
final UtilsShowcase us = new UtilsShowcase();
us.run();
}
public void run(){
forkJoinPoolDemo();
}
public void forkJoinPoolDemo(){
final ForkJoinPool forkJoinPool = new ForkJoinPool();
Future<String> result = forkJoinPool.submit(new MyTask(8));
try {
result.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class MyTask extends RecursiveTask<String> {
final long num;
MyTask(long number){
this.num = number;
}
protected String compute(){
System.out.println("In compute with number of "+num);
if (num > 1){
System.out.println("Generating two more tasks with number of "+num/2);
(new MyTask(num/2)).fork();
(new MyTask(num/2)).fork();
}
return "Returning from a number "+num;
}
}
}
我原以为它会从数字 8 开始,然后为数字 4 启动两个任务,为数字 2 启动 4 个任务,为数字 1 启动 8 个任务。相反,它打印出以下内容:
In compute with number of 8
Generating two more tasks with number of 4
In compute with number of 4
Generating two more tasks with number of 2
In compute with number of 4
Generating two more tasks with number of 2
In compute with number of 2
Generating two more tasks with number of 1
In compute with number of 2
Generating two more tasks with number of 1
In compute with number of 1
In compute with number of 2
In compute with number of 1
它的部分内容是正确的 - 它打印出 2 个数字 4 的任务,但只有 3 个数字 2(而不是 4)和 2 个数字 1(而不是 8!)
非常感谢!
最佳答案
啊,在这里回答我自己的问题。
意识到主线程可能在其他线程完成所有工作之前退出,并且由于 ForkJoinPool 是一个守护进程,它只是默默地结束了。如果我添加
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在方法 forkJoinPoolDemo 的末尾,我得到了我期望的结果:
In compute with number of 8
Generating two more tasks with number of 4
In compute with number of 4
Generating two more tasks with number of 2
In compute with number of 2
Generating two more tasks with number of 1
In compute with number of 4
In compute with number of 1
Generating two more tasks with number of 2
In compute with number of 2
Generating two more tasks with number of 1
In compute with number of 2
Generating two more tasks with number of 1
In compute with number of 1
In compute with number of 1
In compute with number of 1
In compute with number of 1
In compute with number of 1
In compute with number of 2
Generating two more tasks with number of 1
In compute with number of 1
In compute with number of 1
恢复理智:-)
关于java - 为什么 ForkJoinPool 没有运行所有任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27138602/
在我的代码中,我有一个包含静态最终变量的类 private static final ForkJoinPool pool = new ForkJoinPool(availableCPUs - 1);
为什么要为 Scala fork ForkJoinPool? 哪种实现方式和哪种情况更受欢迎? 最佳答案 scala 库拥有自己的 ForkJoinPool 副本的明显原因是 scala 必须在 1.
这个问题在这里已经有了答案: scala.concurrent.forkjoin.ForkJoinPool vs java.util.concurrent.ForkJoinPool (1 个回答)
相关:CompletableFuture on ParallelStream gets batched and runs slower than sequential stream? 我正在研究通过
我正在使用 ForkJoinPool 调查应用程序中的一些性能问题。我们已经与 Dynatrace 进行了合作,有迹象表明一些阻塞操作持续时间太长。我在 FJP 文档或其他地方找不到足够的信息来了解如
嗨,我是 Java 并发的新手,我正在尝试通过 fork join 并将任务分成多个部分来使列表内容加倍。任务已完成,但结果从未到达。 package com.learning; import jav
我是 Java 的新手,正在尝试学习 fork/join 框架。我在网上看到下面的代码,但是,在我运行代码之后,输出似乎是乱序的。所以我只是想知道是否有任何方法可以打印出每个工作线程的 ID?输出非常
我从未使用过 ForkJoinPool,但我偶然发现了这段代码。 我有一个 Set docs .文档有一个写方法。如果我执行以下操作,是否需要 get 或 join 以确保集合中的所有文档都已正确完成
我正在尝试使用 Java 流和 ForkJoinPool 并行化 for 循环,以控制使用的线程数。当使用单线程运行时,并行代码返回与顺序程序相同的结果。顺序代码是一组标准的 for 循环: for(
我正在使用 fork/join 编写 Java 多线程程序。当我调用 fork/join pool 两次时,它只会执行一次,为什么? public class Test extends Recursi
我正在比较测试程序的两个变体。两者都使用 4 线程运行 ForkJoinPool在具有四个内核的机器上。 在“模式 1”中,我使用池非常像执行程序服务。我将一堆任务扔到 ExecutorService
我想了解在 Java fork-join 池中处理任务的顺序。 到目前为止,我在文档中找到的唯一相关信息是关于一个名为“asyncMode”的参数,“如果此池对 fork 任务使用本地先进先出调度模式
演示问题的简单测试: package com.test; import java.util.ArrayList; import java.util.List; import java.util.con
阅读关于 ForkJoinPool 后,我尝试了一个实验来测试与普通递归相比,ForkJoinPool 的实际速度有多快。 我递归地计算了一个文件夹中的文件数量,令我惊讶的是,普通递归比 ForkJo
我刚刚在取消 ForkJoinPool 返回的 Future 时注意到以下现象.给定以下示例代码: ForkJoinPool pool = new ForkJoinPool(); Future fut
ForkJoinPool 是Java 7 中引入的 fork/join 框架的核心之一。它解决了一个常见的问题: 如何在递归中生成多个任务。因为,即使是使用一个简单的 ThreadPoolExecut
我想在 Java 8 中尝试 ForkJoinPool,所以我编写了一个小程序来搜索给定目录中名称包含特定关键字的所有文件。 程序 : public class DirectoryService {
我已经从'org.codehaus.gpars:gpars:1.0.0'更新为'org.codehaus.gpars:gpars:1.1.0'。我的代码在1.0.0中可以正常工作,但是在1.1.0版中
我的代码中有 parallelStreams(),它使用 ForkJoinPool . 线程池执行器有 4 个预定义的处理程序策略,我想知道公共(public)池中默认使用哪一个(如果有的话)。我在文
我有如下 Java 代码: Stream stream = getStreamFromSomewhere() ForkJoinPool pool = new ForkJoinPool(32); poo
我是一名优秀的程序员,十分优秀!