Monitoring Java Pool Executors

The ThreadPoolExecutor class in Java SE 6 documents has the following method :

public int getActiveCount()

Returns the approximate number of threads that are actively performing tasks.

What is the meaning of rough and active execution here?

Is there any guarantee that if earlier, during and after the call getActiveCount()

  • N threads are allocated from the pool to complete the task, and
  • None of these N threads are available for further task assignment,

will the integer returned by getActiveCount() be equal to N?

If getActiveCount() does not provide this guarantee, is there another way to get this information in a more accurate way?

Prior SO Questions:

I looked at the Requirement for monitoring pool thread executors and How to find out if there is an available stream in a thread pool in java , but they do not respond to my requests.

+7
source share
2 answers

The reason it is approximate is because the number may change during the calculation; you are multithreading. Before the calculation is completed, a different number of threads can now be activated (now a thread is active that was inactive when it was installed).

When you say "a specific instance of time" ... it really means nothing. The calculation is not instantaneous. The number you return is the best answer given the fluidity / dynamics of the pool.

If by chance a start starts and ends while none of the threads are in a changing pool state, then yes, this number is “exact”, but only until the thread in the pool changes, which means that it can be “accurate” , for 1 ms (or less).

+9
source

I think that you are possibly confusing things by introducing the concept of "reunite with the pool", which does not actually exist when implementing ThreadPoolExecutor.

Each worker thread is constantly waiting for a task (it effectively sits on top of the blocking queue). Each task includes, in turn, that the worker is “blocked”, then all homework is started, then the actual task is performed, and then postoperative housekeeping, then the worker is “unlocked”.

activeCount () gives you the number of threads in the "locked" state: note that this means that they can actually do housekeeping at the moment the activeCount () function is called, but this is considered to be "active", the task should actually be involved which should be, currently or just completed.

Whether this agrees with your notion of "rejoining the pool", I'm not sure ... as I said, you seem to be inventing the notion that, strictly speaking, does not exist from the point of view of ThreadPoolExecutor.

+2
source

All Articles