[英]Cached Thread Pool growing infinitely
我正在嘗試在Java中實現一個緩存的線程池,以讀取來自總線的數據。 只要我有那條總線上的數據,一切都很好。
如果我斷開數據電纜的連接,程序將開始從池中生成無盡的線程。 我間歇地檢查/ proc / {PID} / fd,它在大約一個小時內從8變為100+。 最終系統崩潰。
我正在為這些線程提交超時值(30秒),它們確實觸發了TimeoutException Catch,該異常在我的日志文件中看到。 這些線程在超時時不應該結束嗎?
一些代碼:
private static ExecutorService executorService = Executors.newCachedThreadPool();
(我稍后提供一個可通話對象)
long[] rawFrame;
Future<long[]> future = executorService.submit(callable);
try {
rawFrame = future.get(timeout, timeUnit); // timeout is 30 seconds
// Do some things with the raw frame and store as parsedFrame
return parsedFrame;
} catch (TimeoutException e) {
LOGGER.error("Bus timeout. Check connection");
return null;
} finally {
future.cancel(true);
}
我只是在學習如何用Java進行並發處理,但是據我所知,這些進程應該超時了,但相反,它們似乎只是坐在那里等待總線上的數據,這些數據不會隨地吐痰。
我想念什么?
編輯:感謝您幫助我縮小范圍。
這是我的電話
private class BusCallable implements Callable<long[]> {
private long messageId;
private boolean readAnyFrame = false;
public BusCallable() {
super();
readAnyFrame = true;
}
public BusCallable(long id) {
super();
this.messageId = id;
readAnyFrame = false;
}
@Override
public long[] call() throws Exception() {
if (readAnyFrame) {
return read(busInterface);
}
return readFrame(busInterface, messageId);
}
超時調用Future.get()將使get()超時,而不是線程超時(即,whataver在可調用對象的call()方法中)。 如果您的可調用項沒有結束,它將保留在服務中,如果您繼續添加更多,則可調用項將累積。 您應該在可調用線程中設置超時機制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.