Java threadpool oversubscription
I have mainly CPU intensive operation which is running on a thread pool. Operation however has certain amount of waiting foe external events which doesn't happen uniformly in time. Since in Java, as far as I know, there is not a thread pool implementation which automatically sizes its number of threads based on observed task throughput (as in Microsoft's CLR 4), is there at least a way to manually tell to thread pool to increase its size when a blocking operation starts and to decrease when it ends?
For example with 8 开发者_StackOverflow社区cores, pool size is 8. If operation is 100% CPU bound, just use this fixed pool. If there is some blocking operation, one should be able to do this:
pool.increase();
waitForSpecialKeyPress();
pool.decrease();
Here is how it is being done in Microsoft's C++ Async library: Use Oversubscription to Offset Latency
You could extend ThreadPoolExecutor
to add your own increase()
and decrease()
functions, which do simple setMaximumPoolSize(getMaximumPoolSize() +/- 1)
.
Make sure to synchronize the methods, to make sure you don't mess up the pool size by accident.
Java 7's ForkJoinPool has a ManagedBlocker, which can be used to keep the pool informed about blocked threads, so that it can schedule more threads if necessary.
EDIT: I forgot to mention, the classes are also available for Java 6 as jsr166y.
精彩评论