I use ExecutorService to implement a pool with three threads and CountDownLatch to control the completion of all threads for further processing.
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); }
I used countDownLatch.await() to wait for all threads to complete. I want this countDownLatch.await() process to countDownLatch.await() if TIME OUT says 45 seconds. How to implement this?
source share