machinery
machinery

Reputation: 6290

Removing idle threads from thread pool in Java?

I'm using a Java thread pool of fixed size (ExecutorService). Let's assume that I submit a job to the thread pool and the job gets idle.

Is there a possibility that idle jobs are removed from the thread pool, so that other jobs from the queue can be processed and then the idle job is later added again?

Upvotes: 3

Views: 5315

Answers (2)

Ravindra babu
Ravindra babu

Reputation: 38950

If you move from ExecutorService to ThreadPoolExecutor, you can achieve it with below API

public void setCorePoolSize(int corePoolSize)

Sets the core number of threads. This overrides any value set in the constructor. If the new value is smaller than the current value, excess existing threads will be terminated when they next become idle.

If larger, new threads will, if needed, be started to execute any queued tasks.

If you want to re-size the pool at run time,

((ThreadPoolExecutor)service).setCorePoolSize(newLimit); //newLimit is new size of the pool

Other API:

public void setMaximumPoolSize(int maximumPoolSize)

Sets the maximum allowed number of threads. This overrides any value set in the constructor. If the new value is smaller than the current value, excess existing threads will be terminated when they next become idle.

Upvotes: 5

snovelli
snovelli

Reputation: 6058

The fact that a task or a Runnable may be in idle (I suppose waiting for I/O or some other resource) is one of the main reason because is appropriate to use a thread pool.

The amount of thread that you want to use on the other hand, is what you need to tune, in order to allow the task to be processed when some of the thread is blocked waiting for a resource.

The only think you need to do is observe how often the thread are getting into idle and tune the amount of thread accordingly.

Guideline to tune a ThreadPool (from: IBM ThreadPools and Work Queue)

Don't queue tasks that wait synchronously for results from other tasks. This can cause a deadlock of the form described above, where all the threads are occupied with tasks that are in turn waiting for results from queued tasks that can't execute because all the threads are busy.

Be careful when using pooled threads for potentially long-lived operations. If the program must wait for a resource, such as an I/O completion, specify a maximum wait time, and then fail or requeue the task for execution at a later time. This guarantees that eventually some progress will be made by freeing the thread for a task that might complete successfully.

Understand your tasks. To tune the thread pool size effectively, you need to understand the tasks that are being queued and what they are doing. Are they CPU-bound? Are they I/O-bound? Your answers will affect how you tune your application. If you have different classes of tasks with radically different characteristics, it may make sense to have multiple work queues for different types of tasks, so each pool can be tuned accordingly.

Upvotes: 3

Related Questions