Viswa Teja Kuncham
Viswa Teja Kuncham

Reputation: 199

Which is better executor to pass to CompletableFuture supplyAsync Executors.newFixedThreadPool(10) or new ForkJoinPool(10)

I'm using CompletableFuture.supplyAsync, I don't want to use Forkjoinpool.commonpool, I want to pass a pool to it.

Currently I'm creating static pool reference and using it, because I don't want to create pool for every request.

I want to know which of the below 2 ways is best.

private static ExecutorService processingPool = Executors.newFixedThreadPool(10);
CompletableFuture.supplyAsync( () -> process(),processingPool);

or

private static ForkJoinPool processingPool = new ForkJoinPool(10);
CompletableFuture.supplyAsync( () -> process(),processingPool);

Also I read that the ForkJoinPool creates threads in daemon mode. I'm using ".get" blocking call at the end of all processing to combine the results. So my question here is, in case if I use forkJoinPool in the above does the JVM wait till the completion as I used ".get" blocking call or will it exit as they are daemon threads.

Upvotes: 1

Views: 285

Answers (1)

John Bollinger
John Bollinger

Reputation: 180286

So my question here is, in case if I use forkJoinPool in the above does the JVM wait till the completion as I used ".get" blocking call or will it exit as they are daemon threads.

I presume that you are invoking get() in the main thread or some other non-daemon thread. In that case, the VM will not terminate before the invocation of get() completes, so it does not matter before that point whether the threads in your pool are daemon threads or normal ones.

After you get your final results, such that your pool no longer has any tasks to perform, the only real issue is about performing a clean VM shutdown. You should shutdown() or shutdownNow() the pool, in which case you should see little or no difference. If you neglect to shut down the pool, however, then you might find that the version of your program with with a fixed thread pool terminates slowly or not at all.

Upvotes: 1

Related Questions