更新:我用此代码修复了它(仅当您知道任务总数时才有效,我的情况)
int totalThreads = 0;
int finishedThreads = 0;
private void checkExecutorFinished() {
if(finishedThreads == totalThreads) {
// Exceutor threads got finished
System.out.println(shufflingResult);
}
}
private void startMappingPhase() throws Exception {
totalThreads = fileLines.size();
for (String line : fileLines) {
Future future = new ExecutingController().mapping(line);
while (!future.isDone()) {
//
}
shuffle((Collection<Pair<String, Integer>>) future.get());
finishedThreads++;
checkExecutorFinished();
}
}
更新:我的问题很清楚,我需要通过执行者提交来做到这一点;没有其他方法
我正在使用执行程序服务来执行我的任务,我想在线程完成时得到通知,我在谷歌上搜索发现有一种方法,但是通过调用执行程序执行,因为我正在使用执行程序提交来添加我的任务,我没有找到办法做到这一点,所以有办法吗?
最佳答案
executor.submit 返回 Future 对象,Future 有很多实用方法,如 isDone()、get() 等!!
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html
关于java - 等待执行器的线程由执行器提交完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56639875/