3

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?

4
  • What do you mean by "idle"? A task will not yield the thread until it is finished. The executor does not "pre-empt" it. So if you wait in there, it will still consume a thread from the pool.
    – Thilo
    Apr 19, 2016 at 23:56
  • Management of threads is the pool's job, not yours. Set up the pool and let it do the work.
    – duffymo
    Apr 19, 2016 at 23:57
  • Removing idle threads would negate the object pool pattern (unless the pool manages it). Apr 19, 2016 at 23:58
  • It is possible that threads are removed. If a thread throws an Exception, it'll be removed and replaced with a new thread. (Also, you said "fixed size". Some thread pools aren't fixed size and will remove idle threads themselves.)
    – markspace
    Apr 20, 2016 at 0:37

2 Answers 2

5

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.

2
  • Thanks for useful info.... You have 6.5K reputation in 10months.. Really Great sir... Where are you working bro.. I'm working in banglore. Apr 21, 2016 at 5:41
  • I am little bit addicted to stackoverflow visiting it from 283 consecutive days. Hyd. Apr 21, 2016 at 6:14
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.