简体   繁体   English

当ExecutorService中的任何线程/可运行/可调用在等待终止时失败时,如何捕获异常

[英]How to catch an exception when any Thread/Runnable/Callable in an ExecutorService fails while awaiting termination

I currently have code that looks something like this: 我目前有看起来像这样的代码:

public void doThings() {
    int numThreads = 4;
    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    for (int i = 0; i < numThreads; i++) {

        final int index = i;
        Runnable runnable = () -> {

            // do things based on index
        };

        threadPool.execute(runnable);

    }

    threadPool.shutdown();

    try {
        // I'd like to catch exceptions here from any of the runnables
        threadPool.awaitTermination(1, TimeUnit.HOURS);
    } catch (InterruptedException e) {
        Utils.throwRuntimeInterruptedException(e);
    }
}

Basically I create a lot of work to do in parallel and wait for it all to be done. 基本上,我创建了很多并行工作,并等待所有工作完成。 If any of that processing fails I need to know quickly and abort it all. 如果任何处理失败,我需要迅速知道并中止所有处理。 threadPool.awaitTermination doesn't seem to notice if an exception was thrown inside one of the threads. threadPool.awaitTermination似乎没有注意到是否在线程之一中引发了异常。 I just see a stacktrace in the console. 我只是在控制台中看到一个stacktrace。

I don't know a lot about concurrency so I'm a bit lost in all the available interfaces/objects such as Callable , Future , Task , etc. 我对并发性了解不多,所以我对所有可用的接口/对象(例如CallableFutureTask等)都有些Callable

I see that threadPool.invokeAll(callables) will give me a List<Future> and Future.get() can throw exceptions from within the thread, but if I call this on (if the callable throws an exception in its own thread). 我看到threadPool.invokeAll(callables)将给我一个List<Future>Future.get()可以从线程内引发异常,但是如果我调用它(如果可调用项在其自己的线程中引发异常)。 But if I .get each callable I have in a sequential collection then I won't know about the last one failing until all the others have finished. 但是,如果我.get每个调用我有一个有序集合,然后我就不会知道最后一个失败,直到所有的人已经完成。

My best guess is to have a queue on which the runnables put a Boolean for success or failure and then take() from the queue as many times as there are threads. 我最好的猜测是要有一个队列,可运行对象在该队列上放置一个Boolean以表示成功或失败,然后从该队列中take()有线程数的次数。

I feel like this is an inordinate amount of complexity (even just the code I've pasted is somewhat surprisingly long) for what seems like a very common, simple use case. 我觉得这似乎是一个非常普通,简单的用例,其复杂性过高(即使只是我粘贴的代码也有些长)。 And this doesn't even include aborting the runnables when one fails. 而且这甚至不包括在发生故障时中止可运行对象。 There has to be a better way, and as a beginner I don't know it. 必须有更好的方法,作为初学者,我不知道。

I eventually found that ExecutorCompletionService is designed for this. 我最终发现ExecutorCompletionService是为此目的而设计的。 I then wrote the following class to abstract the process and simplify usage a bit: 然后,我编写了以下类来抽象化该过程并稍微简化用法:

import java.util.Iterator;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Wrapper around an ExecutorService that allows you to easily submit Callables, get results via iteration,
 * and handle failure quickly. When a submitted callable throws an exception in its thread this
 * will result in a RuntimeException when iterating over results. Typical usage is as follows:
 *
 * <ol>
 *     <li>Create an ExecutorService and pass it to the constructor.</li>
 *     <li>Create Callables and ensure that they respond to interruption, e.g. regularly call: <pre>{@code
 *     if (Thread.currentThread().isInterrupted()) {
           throw new RuntimeException("The thread was interrupted, likely indicating failure in a sibling thread.");
 *     }}</pre></li>
 *     <li>Pass the callables to the submit() method.</li>
 *     <li>Call finishedSubmitting().</li>
 *     <li>Iterate over this object (e.g. with a foreach loop) to get results from the callables.
 *     Each iteration will block waiting for the next result.
 *     If one of the callables throws an unhandled exception or the thread is interrupted during iteration
 *     then ExecutorService.shutdownNow() will be called resulting in all still running callables being interrupted,
 *     and a RuntimeException will be thrown </li>
 * </ol>
 */
public class ExecutorServiceResultsHandler<V> implements Iterable<V> {

    private ExecutorCompletionService<V> completionService;
    private ExecutorService executorService;
    AtomicInteger taskCount = new AtomicInteger(0);

    public ExecutorServiceResultsHandler(ExecutorService executorService) {
        this.executorService = executorService;
        completionService = new ExecutorCompletionService<V>(executorService);
    }

    public void submit(Callable<V> task) {
        completionService.submit(task);
        taskCount.incrementAndGet();
    }

    public void finishedSubmitting() {
        executorService.shutdown();
    }

    @Override
    public Iterator<V> iterator() {
        return new Iterator<V>() {
            @Override
            public boolean hasNext() {
                return taskCount.getAndDecrement() > 0;
            }

            @Override
            public V next() {
                Exception exception;
                try {
                    return completionService.take().get();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    exception = e;
                } catch (ExecutionException e) {
                    exception = e;
                }
                executorService.shutdownNow();
                executorService = null;
                completionService = null;
                throw new RuntimeException(exception);
            }
        };
    }

    /**
     * Convenience method to wait for the callables to finish for when you don't care about the results.
     */
    public void awaitCompletion() {
        for (V ignored : this) {
            // do nothing
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM