简体   繁体   中英

How to wait for all callables to finish executing before proceeding?

I have the following code hashed out:

public class MyCallable implements Callable<Long> {
    @Override
    public Long call() throws Exception {
        // Do stuff...
    }
}

public class MyController {
    private ExecutorService executor = Executos.newCachedTreadPool();

    public Long concurrentDoStuff() {
        List<MyCallable> workers = makeWorkers();

        List<Long> allResults = new ArrayList<Long>();
        for(MyCallable worker : workers) {
            Future<Long> workerResults = executor.submit(worker);

            try {
                allResults.add(workerResults.get());
            } catch(InterruptedException ie) {
                // Handle...
            } catch(ExecutionException ee) {
                // Handle...
            }
        }

        // Question: how do I pause here and wait for all workers to finish?
    }
}

After the for -loop, I want to wait for all workers to finish before proceeding any further. What's the best/safest/most-efficient way to do this? Thanks in advance!

Use a CountDownLatch .

  • Initialize it with the number of workers
  • Pass a reference to the latch in the worker constructor
  • When the worker is done, call countDown
  • Call await in the main thread and it will block until the workers are done.

This is a more general-purpose solution than using methods on the executor service.

您必须使用shutDown关闭Executor,然后等待所有作业都使用awaitTermination进行处理。

You can call:

executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE , TimeUnit.NANOSECONDS);

This will wait (almost) indefinitely for the tasks to complete.

There is a handy method.

ExecutorService.invokeAll(List<Callable> callables);

It will return List<Future> objects so that you can get the returned objects of all your individual call() methods.

You can get the instance of ExecutorService by calling Executors.new....()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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