gpt4 book ai didi

Java ForkJoinPool 不能调用两次?

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:56 27 4
gpt4 key购买 nike

我正在使用 fork/join 编写 Java 多线程程序。当我调用 fork/join pool 两次时,它只会执行一次,为什么?

public class Test extends RecursiveAction{

public static void main(String[] args) {
Test test=new Test();
test.myCompute();
// calling the 2nd time, no output
test.myCompute();
}

public void myCompute() {
ForkJoinPool fjPool = new ForkJoinPool();
fjPool.invoke(this);
}

@Override
public void compute() {
System.out.println("mark");
}

}

输出:标记

最佳答案

这是因为您多次提交相同的任务实例以供执行。每个实例只能执行一次(除非您在再次提交之前使用 reinitialize() 方法...但是,您通常只会创建一个新的任务实例)。

关于Java ForkJoinPool 不能调用两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52583568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com