Java 了解线程池中是否仍在运行线程

Java 了解线程池中是否仍在运行线程,java,multithreading,executorservice,Java,Multithreading,Executorservice,我有以下代码部分: protected ExecutorService parallelExecutor = Executors.newCachedThreadPool(); protected ExecutorService serialExecutor = Executors.newSingleThreadExecutor(); List<?> parallelCommands = new ArrayList<?>(); List<?> serialCom

我有以下代码部分:

protected ExecutorService parallelExecutor = Executors.newCachedThreadPool();
protected ExecutorService serialExecutor = Executors.newSingleThreadExecutor();
List<?> parallelCommands = new ArrayList<?>();
List<?> serialCommands = new ArrayList<?>();
List<Future<Boolean>> results = null;
LocalDateTime timed = LocalDateTime.now().plusSeconds(60);

results = parallelExecutor.invokeAll(parallelCommands);
results.addAll(serialExecutor.invokeAll(serialCommands));
我如何验证执行者是否完成了他们的工作?

JDK文档:

void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
    // Wait a while for existing tasks to terminate
    if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
       pool.shutdownNow(); // Cancel currently executing tasks
       // Wait a while for tasks to respond to being cancelled
       if (!pool.awaitTermination(60, TimeUnit.SECONDS))
          System.err.println("Pool did not terminate");
    }
} catch (InterruptedException ie) {
    // (Re-)Cancel if current thread also interrupted
    pool.shutdownNow();
    // Preserve interrupt status
    Thread.currentThread().interrupt();
}


使用计数器跟踪完成的每个任务。您可以通过修改添加到任务列表中的任务或使用
CompletableFuture
来减少和检查

List<Callable<?>> tasks = ...
ExecutorService executor = ...

// Might want to add the size of your other task list as well
AtomicInteger counter = new AtomicInteger(tasks.size());

for (Callable<?> callable : tasks) {
    results.add(executor.submit(new Callable() {
        callable.call();
        int value = counter.decrementAndGet();

        if (value == 0) {
            synchronized (this) {
                OuterClass.this.notify();
            }
        }
    });
}
long timed = System.currentTimeMillis();

synchronized (this) {
    long timeLeft;

    // Or however many millis your timeout is
    while ((timeLeft = 60_000 - System.currentTimeMillis() - timed) > 0) {
        this.wait(timeLeft);
    }
}

ListCall
get
。已完成的将立即返回
get
也可以通过超时设置进行调用:可能还有一种使用CompleteableFuture执行此操作的奇特方法……这只有在关闭池时才有意义。我尝试检查executor isTerminated()是否正确,但只有当任务完成并随后关闭时,才会返回true。在这种情况下,似乎没有发生池关闭。
List<Callable<?>> tasks = ...
ExecutorService executor = ...

// Might want to add the size of your other task list as well
AtomicInteger counter = new AtomicInteger(tasks.size());

for (Callable<?> callable : tasks) {
    results.add(executor.submit(new Callable() {
        callable.call();
        int value = counter.decrementAndGet();

        if (value == 0) {
            synchronized (this) {
                OuterClass.this.notify();
            }
        }
    });
}
long timed = System.currentTimeMillis();

synchronized (this) {
    long timeLeft;

    // Or however many millis your timeout is
    while ((timeLeft = 60_000 - System.currentTimeMillis() - timed) > 0) {
        this.wait(timeLeft);
    }
}