user3002369
user3002369

Reputation: 43

Java future<object> pooling

I have a very specific application architecture question.

I need to resolve a large amount of incoming destinations, the resolution of these objects is handled asynchronously and when complete the objects need to be passed to the next phase.

So the real question boils down to, what is an elegant way of coping with a large amount of Future objects.

Should I just throw these into a list, iterate over that list and remove them when done? Due to the constant concurrent access the list will most likely be livelocked and become a large bottleneck.

Sorry if my question is phrased rather vaguely.

Upvotes: 2

Views: 330

Answers (4)

Zarathustra
Zarathustra

Reputation: 2943

Have a look at this post: https://stackoverflow.com/a/24363156/1248724

Since you want to wait for all task to finish till you can do the next phase with their results you are looking for a CountDownLatch.

Here an example using an ExecutorService:

CountDownLatch latch = new CountDownLatch(totalNumberOfTasks);
ExecutorService taskExecutor = Executors.newFixedThreadPool(4);
while(...) {
  taskExecutor.execute(new MyTask());
}

try {
  latch.await();
} catch (InterruptedException E) {
   // handle
}

and within your task (enclose in try / finally)

latch.countDown();

Upvotes: 1

Ruben
Ruben

Reputation: 4056

Have a look at RXJava

https://github.com/Netflix/RxJava/wiki

It provides methods to easily handle (transform, filter, combine...) multiple asynchronous tasks.

Upvotes: 0

Alexei Kaigorodov
Alexei Kaigorodov

Reputation: 13525

CompletableFuture from Java8 has methods to organize task chains. In particular,

allOf(CompletableFuture<?>... cfs)
Returns a new CompletableFuture that is completed when all of the
given CompletableFutures complete.

So, collect all CompletableFuture's of the first stage in an array cfs, and call

CompletableFuture.allOf(cfs).thenRunAsync(nextPhase)

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533500

If you need something to be done after the task is complete, I would add that work to the task. i.e. instead of adding a task, add a Runnable which performs both the task and what you need to do with the result. This way the processing of the result is timely and concurrent.

Upvotes: 2

Related Questions