eri*_*ric 6 java concurrency multithreading
ThreadPoolExecutor#getActiveCount()的javadoc 表示方法"返回正在执行任务的大致线程数".
是什么让这个数字接近,而不是确切?是否会报告活动线程?
这是方法:
/**
* 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();
}
}
Run Code Online (Sandbox Code Playgroud)
biz*_*lop 12
该方法获取工作者列表并计算被锁定的工作者.
到计数到达列表末尾时,之前计算过的一些工人可能已经完成了.(或者一些未使用的工人可能已被赋予任务.)
但是你不应该依赖这种知识作为客户,只是这是一种尽力而为的近似.请注意,这种"不准确"不是草率实现的结果,它是每个真正的多线程系统中固有的.在这样的系统中,没有"现在"的全球时刻.即使你停止所有工人计算它们,当你返回结果时,它可能是不准确的.
| 归档时间: |
|
| 查看次数: |
921 次 |
| 最近记录: |