Is it safe to use singleton ExecutorService and multiple instances of CompletionService using the same thread pool?
CompletionService<Object> collector = new ExecutorCompletionService<Object>(threadPool);
So, there will be multiple threads creating instances like above, ‘collector’ with one singleton threadPool.
It will be fine. Each instance of
ExecutorCompletionServicemaintains its own queue of completed tasks and just uses the underlyingExecutorto process each task.The tasks may interfere with each other performance-wise if the number of completion services is large and the thread pool has an upper limit but that won’t affect the correctness of the result.