ThreadPoolExecutor#getActiveCount()的Javadocs说,该方法“返回正在执行任务的线程的近似数量。”
是什么造成了这个数字是近似值而不是精确值?它会低估或高估活跃线程吗?
以下是该方法:
/**
* 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();
}
}