When is it a good idea to create more than one executor service per run of a program/application? Why would you do it, instead of just starting up executors.newcachedthreadpool()
at the beginning and submitting all callables to it.
Below are some benefits: Executor service manage thread in asynchronous way. Use Future callable to get the return result after thread completion. Manage allocation of work to free thread and resale completed work from thread for assigning new work automatically.
The ExecutorService helps in maintaining a pool of threads and assigns them tasks. It also provides the facility to queue up tasks until there is a free thread available if the number of tasks is more than the threads available.
If there are too many jobs queued up, you can use a fixed size BlockingQueue in the ExecutorService to limit the number of items that can be queued up. Then when you try to queue a new task, the operation will block until there is room in the queue.
Creating an ExecutornewCachedThreadPool() — An ExecutorService with a thread pool that creates new threads as required but reuses previously created threads as they become available. Executors. newFixedThreadPool(int numThreads) — An ExecutorService that has a thread pool with a fixed number of threads.
Bottom line, I can't think of a reason why, for a sufficiently large project, you would want to just use one thread pool for your whole program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With