我有一個REST Webservices通過套接字調用從遺留系統獲取結果來處理它的請求。Executor服務線程
由於有4個插座可用,因此我設置了一個Executorservice
且沒有4個線程排隊等待這些連接。該服務最初運行正常,但性能在一段時間後開始下降;我使用JConsole的,發現它正在創建> 3000線,最後用以下錯誤結束:
SEVERE: Servlet.service() for servlet [jersey-serlvet] in context with
path [/DCardServices] threw exception [java.lang.OutOfMemoryError:
unable to create new native thread] with root cause
java.lang.OutOfMemoryError: unable to create new native thread
問題:
- 爲什麼會出現這麼多線程產生的?
- 在這種情況下,我是否必須關閉Executorservice?我如何在Web服務環境中工作?
以下是我的代碼片段。
ExecutorService spool = Executors.newFixedThreadPool(Integer.valueOf(GlobalUtils.getAppProp().getString("socketPoolSize")).intValue());
ExecutorService executorService = Executors.newFixedThreadPool(4);
public Response queforTdmProcess(JSSigData jsData) {
return sndWSResponse(jsData.processCardResp1(executorService.submit(new TdmSocketRequest(jsData,socketQueue, spool)).get()));
}
public class TdmSocketRequest implements Callable<String> {
Socket s = getSocketFromPool();
/* connection to socket and get data */
retSocketToPool(s);
}
public Socket getSocketFromPool() {
try {
conSocketConsumer sckconsumer = new conSocketConsumer(getSocketQueue());
Future<Socket> future = getSpool().submit(sckconsumer);
Socket s = future.get();
System.out.print("Getting socket " + s.getLocalPort() + " from the pool" + "\n");
return s;
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
public void retSocketToPool(Socket s) {
conSocketProducer sckProducer = new conSocketProducer(getSocketQueue(),s);
System.out.print("Returning socket " + s.getLocalPort() + " to the pool" + "\n");
getSpool().submit(sckProducer);
}
}
非常感謝您的任何建議和幫助!
請改善您的問題中的代碼格式 – larsgrefer