Active threads in ExecutorService

后端 未结 6 1503
盖世英雄少女心
盖世英雄少女心 2020-12-23 19:16

Any ideas how to determine the number of active threads currently running in an ExecutorService?

相关标签:
6条回答
  • 2020-12-23 19:17

    Use a ThreadPoolExecutor implementation and call getActiveCount() on it:

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

    The ExecutorService interface does not provide a method for that, it depends on the implementation.

    0 讨论(0)
  • 2020-12-23 19:19

    The ExecutorService interface does not define a method to examine the number of worker threads in the pool, as this is an implementation detail

    public int getPoolSize()
    Returns the current number of threads in the pool.
    

    Is available on the ThreadPoolExecutor class

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

    But this requires you to explicitly create the ThreadPoolExecutor, rather than using the Executors factory which returns ExecutorService objects. You could always create your own factory that returned ThreadPoolExecutors, but you would still be left with the bad form of using the concrete type, not its interface.

    One possibility would be to provide your own ThreadFactory which creates threads in a known thread group, which you can then count

    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());
        }
    }
    
    0 讨论(0)
  • 2020-12-23 19:20

    Check the sourcecode for Executors.newFixedThreadPool():

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

    ThreadPoolExecutor has a getActiveCount() method. So you might either cast the ExecutorService to ThreadPoolExecutor, or use the above code directly to obtain one. You can then invoke getActiveCount().

    0 讨论(0)
  • 2020-12-23 19:32

    I had same issue so created a simple Runnable to trace a ExecutorService instance.

    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 ######");
            }
    
        }
    }
    

    You can simply use this with your executor to get states of ThreadPool

    Ex

    ExecutorService executorService = Executors.newFixedThreadPool(4);
        executorService.execute(new ExecutorServiceAnalyzer(executorService, 1000));
    
    0 讨论(0)
  • 2020-12-23 19:39

    Assuming pool is the name of the ExecutorService instance:

    if (pool instanceof ThreadPoolExecutor) {
        System.out.println(
            "Pool size is now " +
            ((ThreadPoolExecutor) pool).getActiveCount()
        );
    }
    
    0 讨论(0)
  • 2020-12-23 19:44

    Place a static volatile counter on the thread which is updated whenever the thread is activated and deactivated. Also, see the API.

    0 讨论(0)
提交回复
热议问题