[英]How can I make shutdown work properly with this custom ExecutorService?
[英]Is there a way to make an ExecutorService work recursively?
我犯了以下错误:
Executors.newFixedThreadThreadPool
来创建一个池 Callable
对象列表,以便call
方法依次尝试在同一个线程池上启动任务 invokeAll
将它们转储到队列invokeAll
结果是执行程序服务队列的死锁。
我读过的Javadoc似乎并没有禁止这套活动。 我错过了什么? 有没有办法自定义队列或服务,以便这可以工作?
我对你的问题的理解类似于下面的测试用例,它按照文档记录(正如你所说的那样)并且我很乐意在生产中使用。 你的例子与此有何不同?
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class Scratch {
public static void main(String[] args) throws InterruptedException {
final ExecutorService pool = Executors.newFixedThreadPool(1);
pool.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
pool.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
System.out.println(new Date() + ": Second callable being run.");
pool.shutdown();
return null;
}
});
System.out.println(new Date() + ": First callable going to sleep...");
Thread.sleep(2000);
System.out.println(new Date() + ": First callable finished!");
return null;
}
});
pool.awaitTermination(2, TimeUnit.MINUTES);
}
}
打印类似于:
Mon Feb 20 01:18:00 GMT 2012: First callable going to sleep...
Mon Feb 20 01:18:02 GMT 2012: First callable finished!
Mon Feb 20 01:18:02 GMT 2012: Second callable being run.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.