如何检测ThreadPool中是否有空闲线程?
How to detect if there is any idle thread in a ThreadPool?
我创建了一个值为 5 的线程池。将任务分配给 5 个线程后,worker 应该等待创建线程,直到线程池中有一个线程空闲。那么,如何找到线程池中是否有空闲线程?
提前致谢....
if (thread instanceof ThreadPoolExecutor) {
System.out.println(
"Pool size is now " +
((ThreadPoolExecutor) pool).getActiveCount()
);
}
考虑到 ExecutorService 的名称是线程,这为您提供了活动线程,现在您可以获得空闲线程。
我创建了一个值为 5 的线程池。将任务分配给 5 个线程后,worker 应该等待创建线程,直到线程池中有一个线程空闲。那么,如何找到线程池中是否有空闲线程?
提前致谢....
if (thread instanceof ThreadPoolExecutor) {
System.out.println(
"Pool size is now " +
((ThreadPoolExecutor) pool).getActiveCount()
);
}
考虑到 ExecutorService 的名称是线程,这为您提供了活动线程,现在您可以获得空闲线程。