Reputation: 5602
I have an application where I use Executors for creating a thread pool.
Now, I know we can create a thread pool with n number of threads in it while creating.
But in my case I don't want the pool size to be fixed. I want the application to create more threads in the pool if the users are more and automatically reduce the pool size if the users are less.
Please tell me if I'm vague and you need more information on understanding this.
Cheers!!
Upvotes: 1
Views: 150
Reputation: 4767
In general the ExecutorService has default policies that would provide out-of-the-box values for various tuning parameters for newly created threads.
Based on my understanding of your question, IMO you can control the amount of time idle threads are allowed to live within a thread-pool by using the setKeepAliveTime
method.
You can refer to the documentation here for more details.
Also this link provides good resources to read about thread pools and executors available in Java
Upvotes: 1
Reputation: 44338
You probably want to use Executors.newCachedThreadPool(). Threads are created as needed, and if they go unused for a while, they will self-terminate.
Upvotes: 2