1

我正在使用此代码在不同的 CPU 内核之间划分数百个任务。

    final List<Throwable> errors = Collections.synchronizedList(Lists.<Throwable>newArrayList());

    final ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    for (...) {

        pool.execute(new Runnable() { @Override public void run() {

            try {

                // TASK HERE

            } catch (Throwable e) {
                errors.add(e);
            }

        }});

    }

    pool.shutdown();
    try {
        pool.awaitTermination(1000, TimeUnit.DAYS); // wait "indefinitely"
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }

    if (!errors.isEmpty()) throw Exceptions.wrap(errors.get(0)); // TODO multi-exception

它有效,但它并不好。

  • 没有awaitTermination没有超时的版本,这就是我想要的。
  • 我需要自己收集错误。

这样做的正确/常用方法是什么?

4

3 回答 3

3

线程池的重点是重用线程。您应该在应用程序启动时在创建任务的代码之外创建它,然后注入它。添加任务后无需关闭池。当您的应用程序关闭时,您会这样做。

To run a collection of tasks, use ExecutorService.invokeAll. To get the results afterwards, call get on each of the returned Futures. It will rethrow any exception that the task threw, so you can collect it afterwards.

于 2012-08-08T09:37:45.857 回答
1

You can use a future to do the error handling:

final List<Future> futures = new ArrayList<Future>();
for (int i = 0; i < 5; i++) {

    futures.add(pool.submit(new Runnable() { @Override public void run() {
            // TASK HERE
    }}));

}

for (Future f : futures) {
    try {
        f.get();
    } catch (ExecutionException e) {
        //something bad happened in your runnable
    }
}

//when you are done with the executor

pool.shutdown();
try {
    pool.awaitTermination(1000, TimeUnit.DAYS); // wait "indefinitely"
} catch (InterruptedException e) {
    throw new RuntimeException(e);
}
于 2012-08-08T09:38:21.540 回答
0

I think you need to submit each Runnable, get a Future back, and then call get() on each Future.

When you call get(), you'll either get the result of the Runnable, or the exception that it encountered.

于 2012-08-08T09:38:13.940 回答