如何让主线程等待执行程序服务线程完成
how to make main thread wait for executor service threads to complete
这是我的主线程,在每个进程中我调用执行程序服务线程。在第 4 行,在更新状态之前,我想等待所有线程完成
line 1- missingStrategyContext.process(FileType.CARD, programId, fromDate, toDate, channel);
line 2- missingStrategyContext.process(FileType.TRANSACTION, programId, fromDate, toDate, channel);
line 3- missingStrategyContext.process(FileType.REFUND, programId, fromDate, toDate, channel);
line 4- processingLog.setProcessingStatus(TransactionState.SUCCESS.name());
在每个进程中,我正在决定我必须调用哪个策略的文件类型值
public void process(FileType fileType, String programId, Long fromDate, Long toDate, String channel){
map.get(fileType).process(programId, fromDate, toDate, channel, fileType);
}
然后根据文件类型我实现了我的处理方法并在每个文件类型实现中调用了执行器服务
@Override
public void process(String programId, Long fromDate, Long toDate, String channel, FileType fileType) {
MultiTenantTxMgmt multiTenantTxMgmt = MultiTenantTxMgmtUtil.get(programId);
EntityManager entityManager = null;
List<MissingReason> reasonList = new ArrayList<>();
try {
entityManager = multiTenantTxMgmt.getEntityManager();
reasonList = missingDataRepository.getDistinctMissingReasonForFileType(entityManager, fileType, TransactionState.READYTOATTEMPT);
}catch (Exception exception) {
logger.error(exception.getMessage(), exception);
throw new UnkownBatchSaveException();
} finally {
entityManager.close();
}
reasonList.forEach(
missingReason -> deExecutorService.dotask(() ->
missingTransactionStrategyContext.processMissingV3(missingReason, programId, fromDate, toDate, channel, fileType)
)
);
}
您可以使用 CountDownLatch#await
。例如从文档中复制:
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(N);
for (int i = 0; i < N; ++i) // create and start threads
new Thread(new Worker(startSignal, doneSignal)).start();
doSomethingElse(); // don't let run yet
startSignal.countDown(); // let all threads proceed
doSomethingElse();
doneSignal.await(); // wait for all to finish
你可以让 doTask
方法让 return 一个(可完成的)未来(如果它还没有)和 return 方法 process
中的一个未来列表,例如替换
reasonList.forEach(
missingReason -> deExecutorService.dotask(() ->
missingTransactionStrategyContext.processMissingV3(missingReason, programId, fromDate, toDate, channel, fileType)
)
);
和
final List<Future> futures = reasonList.stream()
.map(missingReason -> deExecutorService.dotask(() ->
missingTransactionStrategyContext
.processMissingV3(missingReason, programId, fromDate, toDate, channel, fileType)))
.collect(Collectors.toList());
}
return futures;
然后在您的调用部分,您可以等待所有这些期货终止,然后再继续。
这是我的主线程,在每个进程中我调用执行程序服务线程。在第 4 行,在更新状态之前,我想等待所有线程完成
line 1- missingStrategyContext.process(FileType.CARD, programId, fromDate, toDate, channel);
line 2- missingStrategyContext.process(FileType.TRANSACTION, programId, fromDate, toDate, channel);
line 3- missingStrategyContext.process(FileType.REFUND, programId, fromDate, toDate, channel);
line 4- processingLog.setProcessingStatus(TransactionState.SUCCESS.name());
在每个进程中,我正在决定我必须调用哪个策略的文件类型值
public void process(FileType fileType, String programId, Long fromDate, Long toDate, String channel){
map.get(fileType).process(programId, fromDate, toDate, channel, fileType);
}
然后根据文件类型我实现了我的处理方法并在每个文件类型实现中调用了执行器服务
@Override
public void process(String programId, Long fromDate, Long toDate, String channel, FileType fileType) {
MultiTenantTxMgmt multiTenantTxMgmt = MultiTenantTxMgmtUtil.get(programId);
EntityManager entityManager = null;
List<MissingReason> reasonList = new ArrayList<>();
try {
entityManager = multiTenantTxMgmt.getEntityManager();
reasonList = missingDataRepository.getDistinctMissingReasonForFileType(entityManager, fileType, TransactionState.READYTOATTEMPT);
}catch (Exception exception) {
logger.error(exception.getMessage(), exception);
throw new UnkownBatchSaveException();
} finally {
entityManager.close();
}
reasonList.forEach(
missingReason -> deExecutorService.dotask(() ->
missingTransactionStrategyContext.processMissingV3(missingReason, programId, fromDate, toDate, channel, fileType)
)
);
}
您可以使用 CountDownLatch#await
。例如从文档中复制:
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(N);
for (int i = 0; i < N; ++i) // create and start threads
new Thread(new Worker(startSignal, doneSignal)).start();
doSomethingElse(); // don't let run yet
startSignal.countDown(); // let all threads proceed
doSomethingElse();
doneSignal.await(); // wait for all to finish
你可以让 doTask
方法让 return 一个(可完成的)未来(如果它还没有)和 return 方法 process
中的一个未来列表,例如替换
reasonList.forEach(
missingReason -> deExecutorService.dotask(() ->
missingTransactionStrategyContext.processMissingV3(missingReason, programId, fromDate, toDate, channel, fileType)
)
);
和
final List<Future> futures = reasonList.stream()
.map(missingReason -> deExecutorService.dotask(() ->
missingTransactionStrategyContext
.processMissingV3(missingReason, programId, fromDate, toDate, channel, fileType)))
.collect(Collectors.toList());
}
return futures;
然后在您的调用部分,您可以等待所有这些期货终止,然后再继续。