首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >超时后中止countDownLatch.await()

超时后中止countDownLatch.await()
EN

Stack Overflow用户
提问于 2013-03-06 19:04:27
回答 1查看 13.3K关注 0票数 10

我使用ExecutorService来实现一个三线程池,并使用CountDownLatch来监控所有线程的完成情况,以便进一步处理。

代码语言:javascript
复制
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秒。我如何实现这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-06 19:13:54

使用接受超时的await的重载变体。

代码语言:javascript
复制
countDownLatch.await(45, TimeUnit.SECONDS);
票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15245629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档