Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimizing thread pool size

In his famous concurrency book, Goetz suggests that the optimal number of threads in a pool can be calculated using

N threads = N CPU * U CPU * (1 + W/C)

W/C being the waiting-to-computing-ratio. I want to develop a custom Executor which will adjust worker thread quantity using ThreadPoolExecutor.setCorePoolSize(), but I do not know how to compute the WC-ratio. Assuming that this ThreadPool is the only one used by my application how do I compute that ratio?

Is there a way to track how much time a worker thread is spending on wait/sleep? Or is there any implementation addressing this problem?

like image 429
vach Avatar asked Mar 13 '15 13:03

vach


People also ask

What should be the size of thread pool?

The size of a thread pool is the number of threads kept in reserve for executing tasks. It is usually a tunable parameter of the application, adjusted to optimize program performance. Deciding the optimal thread pool size is crucial to optimize performance.

What is the maximum size of thread pool?

Starting thread pool size is 1, core pool size is 5, max pool size is 10 and the queue is 100. As requests come in, threads will be created up to 5 and then tasks will be added to the queue until it reaches 100. When the queue is full new threads will be created up to maxPoolSize .

What happens when thread pool is full?

By default, the MaxThreads of the ThreadPool is very high. Usually you'll never get there, your app will crash first. So when all threads are busy the new tasks are queued and slowly, at most 1 per 500 ms, the TP will allocate new threads.


1 Answers

http://docs.oracle.com/javase/7/docs/api/java/lang/management/ThreadInfo.html

this class contains methods like

getBlockedCount()

Returns the total number of times that the thread associated with this ThreadInfo blocked to enter or reenter a monitor.

getBlockedTime()

Returns the approximate accumulated elapsed time (in milliseconds) that the thread associated with this ThreadInfo has blocked to enter or reenter a monitor since thread contention monitoring is enabled.

like image 59
Andrei Nikolaenko Avatar answered Sep 19 '22 12:09

Andrei Nikolaenko