Imagine a scenario.
You have a module which processes a certain number of tasks given to it & each task can be executed independently but you want to make sure that this module shouldn't take up the whole CPU to process the given tasks, so that other modules' work is not impacted.
But you can throttle the number of threads that can be used by this module and you want to control the number of minimum threads and maximum threads to use, plus you want to control the idle time of those extra threads created. You can use ThreadPoolExecutor here, where you can provide the threads factory from which threads can be created.
Also, you can select BlockingQueue size to hold the extra tasks which need to be park due to unavailbility of threads and the policy to decide for the extra tasks which can not be stored in the queue(as queue is full) & no free thread is available(as maximum threads are already busy).
Below is one example using a variation of ThreadPoolExecutor but you can try others with other policies also.
But I believe it a very good feature to know about if you work in multithreaded application.
You have a module which processes a certain number of tasks given to it & each task can be executed independently but you want to make sure that this module shouldn't take up the whole CPU to process the given tasks, so that other modules' work is not impacted.
But you can throttle the number of threads that can be used by this module and you want to control the number of minimum threads and maximum threads to use, plus you want to control the idle time of those extra threads created. You can use ThreadPoolExecutor here, where you can provide the threads factory from which threads can be created.
Also, you can select BlockingQueue size to hold the extra tasks which need to be park due to unavailbility of threads and the policy to decide for the extra tasks which can not be stored in the queue(as queue is full) & no free thread is available(as maximum threads are already busy).
Below is one example using a variation of ThreadPoolExecutor but you can try others with other policies also.
But I believe it a very good feature to know about if you work in multithreaded application.
ThreadPoolList
RSS Feed