![](/img/trans.png)
[英]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.