超时后中止countDownLatch.await()
我正在使用ExecutorService
实现一个3线程池,并使用CountDownLatch监视所有线程的完成情况,以进行进一步处理。
ExecutorService threadExecutor = Executors.newFixedThreadPool(3);CountDownLatch countDownLatch = new CountDownLatch(3);
AuthorisationHistoryTask task1 =
new AuthorisationHistoryTask(commonDataThread, countDownLatch );
PreAuthHistoryTask task2 =
new PreAuthHistoryTask(userID,sessionID, commonDataThread, countDownLatch );
SettlementHistTask task4 = new SettlementHistTask(commonDataThread,countDownLatch);
Future<Map<String, Object>> futureAuthHistory = threadExecutor.submit (task1);
Future<Map<String, Object>> futurePreAuthHist = threadExecutor.submit (task2);
Future<Map<String, Object>> futureSettleHist = threadExecutor.submit(task4);
threadExecutor.shutdown();
try {
countDownLatch.await();
}catch (InterruptedException e) {
logger.logCommon("InterruptedException",CLASS_NAME);
}catch (ExecutionException ex) {
logger.logCommon("InterruptedException",CLASS_NAME);
}
我曾经countDownLatch.await()
等到所有线程完成。我希望此过程countDownLatch.await()
在超时(例如45秒)的情况下中止。我该如何实施?
回答:
使用的重载变体await
接受超时。
countDownLatch.await(45, TimeUnit.SECONDS);
以上是 超时后中止countDownLatch.await() 的全部内容, 来源链接: utcz.com/qa/435063.html