I have a task producer which will create a large number of tasks. I want to ensure that threads are re-used because of the large number of tasks.
I initially was using CompletableFuture without specifying any executor, so JVM is using the default executor as ForkJoinPool.commonPool. Since the tasks are blocking, running these on the common pool might affect other service based tasks.
So I was looking into creating a custom executor for this. I looked into this doc : https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/Executors.html
Found these executors : FixedThreadPool and WorkStealingPool
FixedThreadPool seems to be using a single unbounded queue, which the threads will consume tasks from.
WorkStealingPool can use multiple queues for decreasing contention. It can also dynamically increase or decrease the number of threads being used on the basis of tasks.
I feel that the use of multiple queues, and the work stealing algorithm itself seems better than the single queue usage of FixedThreadPool. But the unbounded thread count worries me, as I don't want to create a large number of threads for the large number of tasks, I know are going to be created.
Wanted to understand which is the better option here.