[英]ScheduledExecutorService stops working even when all exceptions are caught
[英]ScheduledThreadPoolExecutor stops executing with caught exceptions
我有以下課程
public class MaintanceTools {
public static final ScheduledThreadPoolExecutor THREADSUPERVISER;
private static final int ALLOWEDIDLESECONDS = 1*20;
static {
THREADSUPERVISER = new ScheduledThreadPoolExecutor(10);
}
public static void launchThreadsSupervising() {
THREADSUPERVISER.scheduleWithFixedDelay(() -> {
System.out.println("maintance launched " + Instant.now());
ACTIVECONNECTIONS.forEach((connection) -> {
try {
if ( !connection.isDataConnected() &&
(connection.getLastActionInstant()
.until(Instant.now(), SECONDS) > ALLOWEDIDLESECONDS)) {
connection.closeFTPConnection();
ACTIVECONNECTIONS.remove(connection);
}
} catch (Throwable e) { }
});
System.out.println("maintance finished " + Instant.now());
}, 0, 20, TimeUnit.SECONDS);
}
}
它遍歷所有的FTP連接(因為我寫了FTP服務器),檢查連接是否沒有傳輸任何數據並且空閑了一段時間,如果是則關閉連接。 問題在於任務永遠不會在中斷線程中引發某些異常后運行。 我知道它是用docs編寫的。如果任務的任何執行遇到異常,都會抑制后續執行。 否則,任務將僅通過取消或終止執行程序而終止。 而且我有一個例外,但是它被捕獲了並且不要在throwing函數之外。 此函數引發AsynchronousCloseException,因為它掛在channel.read(readBuffer);上。 當連接關閉時,引發並捕獲異常。
問題是如何使THREADSUPERVISER正常工作,而不管引發和處理任何異常。
調試輸出:
事實證明,問題出在
ACTIVECONNECTIONS.remove(connection);
我有ConcurrentModifyingException。 http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/中的解決方案效果很好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.