ExecutorService中的活动线程

时间:2008-09-17 07:25:13

标签: java multithreading concurrency

如何确定当前在ExecutorService中运行的活动线程数?

6 个答案:

答案 0 :(得分:63)

使用ThreadPoolExecutor实施并在其上调用getActiveCount()

int getActiveCount() 
// Returns the approximate number of threads that are actively executing tasks.

ExecutorService接口没有提供相应的方法,它取决于实现。

答案 1 :(得分:23)

假设pool是ExecutorService实例的名称:

if (pool instanceof ThreadPoolExecutor) {
    System.out.println(
        "Pool size is now " +
        ((ThreadPoolExecutor) pool).getActiveCount()
    );
}

答案 2 :(得分:21)

检查Executors.newFixedThreadPool()的源代码:

return new ThreadPoolExecutor(nThreads, nThreads,
                              0L, TimeUnit.MILLISECONDS,
                              new LinkedBlockingQueue<Runnable>());

ThreadPoolExecutor有一个getActiveCount()方法。因此,您可以将ExecutorService转换为ThreadPoolExecutor,或者直接使用上面的代码来获取一个。然后,您可以调用getActiveCount()。

答案 3 :(得分:10)

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());
    }
}

答案 4 :(得分:3)

我有同样的问题,因此创建了一个简单的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));

答案 5 :(得分:-4)

在线程上放置一个静态易失性计数器,每当线程被激活和停用时,该计数器都会更新。 另请参阅API。