所以我试图让自己熟悉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;
}
}
据我所知应该无限地打印“线程”,但是当我运行它时,它只能打印“线程”大约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();
}
}
(当然,只是为了说明...)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句