dma_k
dma_k

Reputation: 10639

Spring core helper to wait for all concurrent tasks

There is a collection of tasks given as Callable or Future objects. It is necessary to execute these tasks concurrently with one timeout for all tasks, thus if timeout is 3 seconds then in 3 seconds either all tasks should complete or TimeoutException should be registered.

In JDK the given operation is present as ExecutorService#invokeAll(Collection<? extends Callable<T>>, long, TimeUnit), but since Spring framework hides this interface behind AsyncTaskExecutor it is not possible to benefit from it. Copy-paste of the code is not an option.

What is the best way out? I would suggest that given functionality is implemented as a helper method in some utility class in Spring.

Upvotes: 0

Views: 33

Answers (1)

dezhik
dezhik

Reputation: 1030

Didn't find something useful in spring core.
IMHO the best way is to use CountDownLatch. Pass it into Callable and call countDown() after work is done with await(3, TimeUnit.SECONDS) in 'main' thread. Note that await(long timeout, TimeUnit unit) returns boolean without throwing any TimeoutException.

Upvotes: 1

Related Questions