File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Auto tuning ThreadPool in runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Auto tuning ThreadPool in runtime" Watch "Auto tuning ThreadPool in runtime" New topic

Auto tuning ThreadPool in runtime

Enrico Tamellin

Joined: Nov 09, 2008
Posts: 23

I have this common situation in which I have a set of tasks to execute.
Each tasks has:
- a part that execute a certain code (it spends time Te)
- and a part it has to wait for an amount of time that I call Tw.

In this situation it's good using a ThreadPool of size (1+ Tw/Te).

The problem is that Tw is not constant, and it changes during the time: so I would like to have a System that increase or decrease the size of the Pool given the Tw that I calculate.

And now my question:
Do you know if there are some systems already made that "auto-tune" the size of the ThreadPool? I would like to know if there are solutions already done to that!

Thank you in advance!!! :-)
Peter Lawrey
Ranch Hand

Joined: Dec 21, 2008
Posts: 62
Executors.newCachedThreadPool() tunes the number of threads. See its Javadoc for details. However to tune the number in the number of threads as you suggest you can override submit, beforeExecute and/or afterExecute in ThreadPoolExecutor to do what you want.
Enrico Tamellin

Joined: Nov 09, 2008
Posts: 23
Thanks for your tip! :-)
... but I think I can't use your solution in my application.

The fact is that if I have 1000 requests of executions in a rush, I will have 1000 Thread running in the same time: this could be a problem because it increases the amount of Context Switch.

So if I know exactly the time of execution Te and the time of wait Tw and they are constant, the optimum thread pool size will be (1 + Tw/Te) so I can have a system that use 100% his resources without a relevant Context Switch. I have already done it.

BUT, if I have Tw not constant it will be a problem. I will have to calculate it! So I would like to know if someone know there are classes or solution to this already done in Internet.
I agree. Here's the link:
subject: Auto tuning ThreadPool in runtime
jQuery in Action, 3rd edition