Block until ExecutorService is done [closed]?

Up vote 0 down vote favorite 1 share g+ share fb share tw.

Executor. Execute(task1); executor. Execute(task2); executor.

Execute(task3); executor. Execute(task4); executor. Execute(task5); // ...now I want to block until all tasks have finished executing... System.out.

Println("done! ") java multithreading concurrency blocking executorservice link|improve this question asked Jun 16 '11 at 15:49FredOverflow40.4k857163 100% accept rate.

Use invokeAll, and then call get on the returned Future objects...it's analogous to Thread.join(). – mre Jun 16 '11 at 15:55.

You can use a ThreadPoolExecutor with a pool size set to System.getRuntime(). AvailableProcessors() and .execute() it all the tasks you want to execute, then call tpe.shutdown() and then wait in a while(!tpe.terminated()) { /* waiting for all tasks to complete */} which blocks for all the submitted tasks to complete. Where tpe is a reference to your ThreadPoolExecutor instance.

Or if it is more appropriate use an ExecutorCompletionService A CompletionService that uses a supplied Executor to execute tasks. This class arranges that submitted tasks are, upon completion, placed on a queue accessible using take. The class is lightweight enough to be suitable for transient use when processing groups of tasks.

ExecutorService, how to wait for all tasks to finish.

For all your tasks, put them into a List callables then invokeAll on them ExecutorService e = ... e. InvokeAll(callables); Per javadocs Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future.isDone() is true for each element of the returned list.

Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress. Thus the thread will wait until all tasks haven been completed.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions