In javadocs for ThreadPoolExecutor # getActiveCount () they say that the method "Returns the approximate number of threads that are actively performing tasks."
What makes this number approximate rather than accurate? Will they process or process active threads?
Here is a way:
public int getActiveCount() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
int n = 0;
for (Worker w : workers)
if (w.isLocked())
++n;
return n;
} finally {
mainLock.unlock();
}
}
source
share