[英]Shutdown executor without executorService in Java
有沒有一種方法可以先關閉Java中的Executor
,而無需先將其強制轉換為ExecutorService
? 基本上我有一個叫做Exx
的類,它implements Executor
。 我想關閉Exx。 我該怎么做? 本質上,我需要知道如何關閉Executor
。
編輯:示例:
class Exx implements Executor{
@Override
public void execute(Runnable r) {
new Thread(r).start();
}
}
一個Executor
由特定接口的性質是不是需要被關閉。 例如,這是Executor
最基本的實現。
class NearlyPointlessExecutor implements Executor {
public void execute(Runnable r) {
r.run();
}
}
顯然,在上面的代碼中,沒有什么復雜的事情需要關閉任何東西,但是所提供的類完全遵循Executor
接口。
如果您的實現確實需要關閉,那么您可以選擇創建自己的接口或實現ExecutorService
。
更新問題編輯:
對於提供的代碼,將無法關閉正在創建的線程,因為在集合中沒有保留對它們的引用。
但是,有一種解決方案,提供的實現與使用的實現基本相同: Executors.newCachedThreadPool 。 放棄自己的實現,而改用此實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.