java - 等待执行器的线程由执行器提交完成

标签 java multithreading executorservice

更新:我用此代码修复了它(仅当您知道任务总数时才有效,我的情况)

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/

相关文章:

multithreading - 用于异步 Web 服务的 Java ExecutorService

java - 为什么log4j2不起作用?

绘制多个多边形后Java绘图程序变慢

c# - 使用 CurrentCulture 设置为任务创建者 CurrentCulture 执行任务

java - 在java中模拟ExecutorService invokeAll方法

java - 我的 ExecutorService 实现正确吗?

java - 为什么在ArrayDeque和Stack中使用push()方法时输出不同?

java - 如何解析文本文件并从中创建数据库记录

java线程同步问题

c# - Monitor.TryEnter检查对象是否已锁定?