簡體   English   中英

關閉后,由ThreadPoolExecutor創建的線程保持運行

[英]threads created by ThreadPoolExecutor keeps running after shutdown

我正在嘗試使用兩個threadPoolExecutors應用某些數據的多線程生產者/使用者的原則,我注意到一切正常,但是該程序拒絕關閉,並且在關閉2個threadPools之后某些線程仍在運行:

在主要班級:

ExecutorService executor = new ThreadPoolExecutor(10, 10, 40, TimeUnit.SECONDS,
        new ArrayBlockingQueue<Runnable>(edmsIds.size()));
ExecutorService consumerExecutor =  new ThreadPoolExecutor(10, 10, 0L, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(edmsIds.size()));for(
String edmsID:edmsIds)

{

    Runnable worker = new BatchWorkerThread(resumeFile, stratrKeysToUpdate, xslFiles, errorThrown, edmsID,
            consumerExecutor, edmsIds.size(), executor);
    executor.execute(worker);
}

在生產者類中:

while (edmsNumber > 0) {
    Runnable consumer = new ConsumerThread(brokerWorker);
    consumExecutor.execute(consumer);//decreasing the edmsNumber

}
if (edmsNumber < 1) {
    prodExecutor.shutdownNow();
    consumExecutor.shutdownNow();
}

參見文檔

除了盡最大努力嘗試停止處理正在執行的任務之外, 沒有任何保證 例如,典型的實現將通過Thread.interrupt()取消,因此任何無法響應中斷的任務都可能永遠不會終止。

如果正在運行的任務不可中斷(不響應中斷信號),它將繼續執行。

實際上,我想發布問題的原因和解決方案:


  • 原因

    我正在嘗試關閉未終止的准分子。

  • 僅在終止后使用簡單代碼將其關閉。

例如 :

prodExecutor.shutdownNow(); while (!prodExecutor.isTerminated()) {}

  • 如果要在完成任務時執行任務,則需要ExecutorCompletionService,這是另一種解決方案,即使用ExecutorCompletionService。 這充當BlockingQueue,可讓您在完成任務時輪詢任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM