Kindly help me in understanding the internal flow of newFixedThreadPool (or Cached)
When we write below statements, ExecutorService e=Executors.newFixedThreadPool(3)
When you execute ExecutorService e=Executors.newFixedThreadPool(3);
a thread pool will be created with 3 threads inside it. And these 3 threads will be used to execute any task executed on you e
object.
When you try to execute tasks through ExecutorService
then it will get added to a tasks queue, if number of threads in pool are greater than number of tasks, then as soon as some task will come, some free thread in the pool will be picked and used to execute task.
When number of tasks become greater than number of threads in pool, then they will be added in pipleline in the queue, and as soon as some thread as finished execution, that thread will be used to execute task from the queue pipeline.
Thread pooling:
The thread which has just finished will not terminate. Pleas note that it is a thread pool, so threads are pooled, which means that they will not terminate (typically, until you have some timeout or some other mechanism) but will go back in pool so that they can be reused. And that's the reason your 4 and 5th runnable will be executed through those pooled threads.
"no new thread will be created but work will be waiting for a thread to be free."
This is same as discussed above that then they put be waiting in the queue, and as soon as some thread is done with the task execution, waiting tasks from the queue will be pooled and be executed from the free thread, and this will happen until all the tasks from the queue is cleaned up.
No new thread will be created because you have used newFixedThreadPool
, so a fixed thread pool of 3 threads is created and they only will be used to process all the tasks requested coming to that particular instance of ExecutorService
and in your case it is e
.
Other thread pooling options:
You can create a thread pool in several ways using java.util.concurrent.Executors
. For example, using Executors#newCachedThreadPool()
you can create a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.
Check all available ways to find which best suits you.