How to wait for all tasks in an ThreadPoolExecutor to finish without shutting down the Executor? How to wait for all tasks in an ThreadPoolExecutor to finish without shutting down the Executor? multithreading multithreading

How to wait for all tasks in an ThreadPoolExecutor to finish without shutting down the Executor?


If you are interested in knowing when a certain task completes, or a certain batch of tasks, you may use ExecutorService.submit(Runnable). Invoking this method returns a Future object which may be placed into a Collection which your main thread will then iterate over calling Future.get() for each one. This will cause your main thread to halt execution until the ExecutorService has processed all of the Runnable tasks.

Collection<Future<?>> futures = new LinkedList<Future<?>>();futures.add(executorService.submit(myRunnable));for (Future<?> future:futures) {    future.get();}


My Scenario is a web crawler to fetch some information from a web site then processing them. A ThreadPoolExecutor is used to speed up the process because many pages can be loaded in the time. So new tasks will be created in the existing task because the crawler will follow hyperlinks in each page. The problem is the same: the main thread do not know when all the tasks are completed and it can start to process the result. I use a simple way to determine this. It is not very elegant but works in my case:

while (executor.getTaskCount()!=executor.getCompletedTaskCount()){    System.err.println("count="+executor.getTaskCount()+","+executor.getCompletedTaskCount());    Thread.sleep(5000);}executor.shutdown();executor.awaitTermination(60, TimeUnit.SECONDS);