ExecutorService 中的活动线程
-
09-06-2019 - |
题
任何想法如何确定当前正在运行的活动线程的数量 ExecutorService
?
其他提示
假设 pool
是 ExecutorService 实例的名称:
if (pool instanceof ThreadPoolExecutor) {
System.out.println(
"Pool size is now " +
((ThreadPoolExecutor) pool).getActiveCount()
);
}
检查 Executors.newFixedThreadPool() 的源代码:
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
ThreadPoolExecutor 有一个 getActiveCount() 方法。因此,您可以将 ExecutorService 转换为 ThreadPoolExecutor,或者直接使用上面的代码来获取它。然后您可以调用 getActiveCount()。
ExecutorService 接口没有定义检查池中工作线程数量的方法,因为这是一个实现细节
public int getPoolSize()
Returns the current number of threads in the pool.
在 ThreadPoolExecutor 类上可用
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class PoolSize { public static void main(String[] args) { ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue()); System.out.println(executor.getPoolSize()); } }
但这需要您显式创建 ThreadPoolExecutor,而不是使用返回 ExecutorService 对象的 Executors 工厂。您始终可以创建自己的工厂来返回 ThreadPoolExecutors,但您仍然会遇到使用具体类型而不是其接口的错误形式。
一种可能性是提供您自己的 ThreadFactory,它在已知线程组中创建线程,然后您可以对其进行计数
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; public class PoolSize2 { public static void main(String[] args) { final ThreadGroup threadGroup = new ThreadGroup("workers"); ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() { public Thread newThread(Runnable r) { return new Thread(threadGroup, r); } }); System.out.println(threadGroup.activeCount()); } }
我遇到了同样的问题,因此创建了一个简单的 Runnable 来跟踪 ExecutorService 实例。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
public class ExecutorServiceAnalyzer implements Runnable
{
private final ThreadPoolExecutor threadPoolExecutor;
private final int timeDiff;
public ExecutorServiceAnalyzer(ExecutorService executorService, int timeDiff)
{
this.timeDiff = timeDiff;
if (executorService instanceof ThreadPoolExecutor)
{
threadPoolExecutor = (ThreadPoolExecutor) executorService;
}
else
{
threadPoolExecutor = null;
System.out.println("This executor doesn't support ThreadPoolExecutor ");
}
}
@Override
public void run()
{
if (threadPoolExecutor != null)
{
do
{
System.out.println("#### Thread Report:: Active:" + threadPoolExecutor.getActiveCount() + " Pool: "
+ threadPoolExecutor.getPoolSize() + " MaxPool: " + threadPoolExecutor.getMaximumPoolSize()
+ " ####");
try
{
Thread.sleep(timeDiff);
}
catch (Exception e)
{
}
} while (threadPoolExecutor.getActiveCount() > 1);
System.out.println("##### Terminating as only 1 thread is active ######");
}
}
}
您可以简单地将其与执行器一起使用来获取 ThreadPool 的状态
前任
ExecutorService executorService = Executors.newFixedThreadPool(4);
executorService.execute(new ExecutorServiceAnalyzer(executorService, 1000));
在线程上放置一个静态易失性计数器,每当线程激活和停用时都会更新该计数器。另请参阅 API。
不隶属于 StackOverflow