所以我试图熟悉java中的并发编程,所以我编写了以下代码
public class Test{
public static void main(String[] args){
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(new Agent());
}
}
class thread extends RecursiveTask<Integer>{
thread(){
System.out.println("Thread");
}
public Integer compute(){
thread x = new thread();
x.fork();
return 0;
}
}
据我所知,应该无限地打印“Thread”,但当我运行它时,它只打印“Thread”大约 20 次。
有人知道为什么吗?
最佳答案
主线程将退出,并声明 ForkJoinPool 中线程的生存期。您可以通过
看到这一点public static void main(String[] args)
{
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(new MyRecursiveTask());
try
{
Thread.sleep(10000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
(当然,只是为了说明......)
关于java - 使用 Java RecursiveTasks 创建多个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22580981/