Java Executor服务中的活动线程
如何确定当前在一个应用程序中运行的活动线程的数量?请使用一个实现并调用它:Java Executor服务中的活动线程,java,multithreading,concurrency,Java,Multithreading,Concurrency,如何确定当前在一个应用程序中运行的活动线程的数量?请使用一个实现并调用它: ExecutorService接口没有为此提供方法,这取决于实现。在线程上放置一个静态易失性计数器,每当激活和停用线程时,该计数器都会更新。 另外,请参阅API。检查源代码中的执行器。newFixedThreadPool(): 返回新的ThreadPoolExecutor(nThreads,nThreads, 0L,时间单位为毫秒, 新建LinkedBlockingQueue()); ThreadPoolExecuto
ExecutorService接口没有为此提供方法,这取决于实现。在线程上放置一个静态易失性计数器,每当激活和停用线程时,该计数器都会更新。
另外,请参阅API。检查源代码中的执行器。newFixedThreadPool():
返回新的ThreadPoolExecutor(nThreads,nThreads,
0L,时间单位为毫秒,
新建LinkedBlockingQueue());
ThreadPoolExecutor有一个getActiveCount()方法。因此,您可以将ExecutorService强制转换为ThreadPoolExecutor,或者直接使用上面的代码来获取ExecutorService。然后可以调用
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());
}
}
导入java.util.concurrent.LinkedBlockingQueue;
导入java.util.concurrent.ThreadPoolExecutor;
导入java.util.concurrent.TimeUnit;
公共类池大小{
公共静态void main(字符串[]args){
ThreadPoolExecutor executor=新的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());
}
}
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.ThreadFactory;
公共类池大小2{
公共静态void main(字符串[]args){
最终螺纹组螺纹组=新螺纹组(“工人”);
ExecutorService executor=Executors.newCachedThreadPool(newThreadFactory()){
公共线程newThread(可运行的r){
返回新螺纹(螺纹组,r);
}
});
System.out.println(threadGroup.activeCount());
}
}
假设
pool
是ExecutorService实例的名称:
if (pool instanceof ThreadPoolExecutor) {
System.out.println(
"Pool size is now " +
((ThreadPoolExecutor) pool).getActiveCount()
);
}
我也有同样的问题,所以创建了一个简单的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 ######");
}
}
}
您只需将其与执行器一起使用,即可获取线程池的状态
前
当且仅当运行环境允许实现为线程设置组(ej.googleappengine)时,这是正常的
if (pool instanceof ThreadPoolExecutor) {
System.out.println(
"Pool size is now " +
((ThreadPoolExecutor) pool).getActiveCount()
);
}
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 ######");
}
}
}
ExecutorService executorService = Executors.newFixedThreadPool(4);
executorService.execute(new ExecutorServiceAnalyzer(executorService, 1000));