Elad Benda2
Elad Benda2

Reputation: 15512

how can executor service signal when all tasks are finished?

I read this post

I want to fill an array with all the tasks results

but I want to call a certain method only when all Future have values (aka all tasks have results).

Is there a way to write such a barrier?

Upvotes: 0

Views: 96

Answers (1)

Zielu
Zielu

Reputation: 8562

With executor service you need to shut it down and wait for its termination.

executorService.shutdown();
executorService.awaitTermination(1,TimeUnit.HOUR);

The shutdown does not stop the executor instantly, but it prevents it from taking the new tasks and it allows to complete the already submitted.

The awaitTermination blocks the calling thread till the whole tasks queue in executor is processed.

Another approach is to use ExecutorCompletionService instead and call take() as many times as the number of tasks submitted. Each take will block until a future is completed (or it failed).

Upvotes: 1

Related Questions