Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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

like image 932
Primk Avatar asked Apr 12 '11 16:04

Primk


People also ask

When should you not use ThreadPool?

Thread pools do not make sense when you need thread which perform entirely dissimilar and unrelated actions, which cannot be considered "jobs"; e.g., One thread for GUI event handling, another for backend processing. Thread pools also don't make sense when processing forms a pipeline.

What is a ThreadPool is it better than using several simple threads?

A thread pool is a collection of threads which are assigned to perform uniformed tasks. The advantages of using thread pool pattern is that you can define how many threads is allowed to execute simultaneously.

How many maximum threads can be created using a ThreadPool?

The maximum allowed number of processing threads in a pool is 1023. The pool allocates a maximum of 1000 threads in an I/O operation. To get maximum number of threads, you can use the GetMaxThreads method of the ThreadPool static class.


1 Answers

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.

like image 71
Jonathan Avatar answered Sep 30 '22 05:09

Jonathan