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!
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.
Joined: Nov 09, 2008
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.