I have a class that will run configured number of threads, to read form a BlockingQueue. the threads count will be initialised on load of the class and those many threads will be running at a time.
My threads will be coming back to thread pool only when the server is stopped, as the threads will be waiting on the blocking queue for new data.
At some point of time i would like to dynamically change the thread count. Is there a way to do that. My sample code looks as below.
The variable workerThreads is initially assigned to 2.
Inside the run method i would like to read this variable again form some config and get the value.
Then as per that value i would like to increase or decrease the number of QueueProcessor threads running at a time.
Please let me know any one have worked in similar scenarios and the best feasible solution that can be provided.
If you want to control the number of threads, you should at a minimum have a list to store references to those Thread objects, so that you can interrupt them when reducing the pool.
Instead of implementing all that yourself, I think you should use Executors.newCachedThreadPool(). It'll terminate any excess threads which are idle for some period of time without you having to do anything extra.