My understanding of thread pool is that that when you pass a thread pool a method it picks a thread from its active pool and sends the method off to be executed. My understanding of a thread is that if you put some code in it, it will run through the code without stopping until it is told to sleep or stop.
My question:
When you pass several methods (of roughly equal length to execute) to a thread pool one after another does the thread pool manage sharing between the different threads being executed so that the tasks finish almost simultaneously or does it execute them one after another – for example, task 1 goes into thread 1, thread 1 starts executing it and thread 2 with task 2 in it hasn’t had a chance to run until task task 1 has finished because thread 1 was running through the code it has been given without any explicit messages to sleep – so then thread 2 will execute and the tasks will essentially finish one after another.
Basically, do I have to explicitly make sure the code I give to a thread pool has a breaks in it for the threads to run simultaneously? Or do I get this for free when using a thread pool manager?
1. The number of Thread you specify in the pool works simultaneously, thats what threads are for, to simultaneously do the work. Its a Parallel Run
2. For example FixedThreadPool, will have certain nos of threads, when you give them a certain nos of tasks to be performed, all runs simultaneously to do it, if the number of work is less than the threads in the pool, then only those many thread which are needed to perform the task will run simultaneously.
3. In case of CachedThreadPool, the number of threads in the pool run simultaneously to complete the work, if there is a need of more thread, CachedThreadPool will create new ones or till then it reuses it.