The ThreadPoolExecutor class in the Java SE 6 docs has the following method:
public int getActiveCount()Returns the approximate number of threads
that are actively executing tasks.
What is the meaning of approximate and actively executing here?
Is there any guarantee that, if before, during and after the call to getActiveCount()
- N threads have been allotted from the pool for task execution, and
- None of these N threads are available for further task assignment,
the integer returned by getActiveCount() will be exactly N?
If getActiveCount() does not provide this guarantee, is there any other way to obtain this information in a more precise manner?
Prior SO Questions:
I have looked at Thread Pool Executor Monitoring Requirement and How to tell if there is an available thread in a thread pool in java, but they do not answer my queries.
The reason that it is approximate is because the number could change during the calculation; you’re multi-threading. Before the calculation completes a different number of threads could now be active (A thread that was inactive when checked is now active).
When you say “particular time instance” … that doesn’t really mean anything. The calculation isn’t instantaneous. The number you get back is the best possible answer given the fluid/dynamic nature of the pool.
If by chance the calculation starts and completes while none of the threads in the pool change state, then yes that number is “exact” but only until a thread in the pool changes state, which means it might only be “exact” for 1ms (or less).