How "approximate" is ThreadPoolExecutor # getActiveCount ()?

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:

/**
 * Returns the approximate number of threads that are actively
 * executing tasks.
 *
 * @return the number of threads
 */
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();
    }
}
+4
source share
1 answer

The method takes a list of workers and counts blocked workers.

, , . ( , , ​​.)

, , . , "" , . "". , , .

+7

All Articles