Active Topics in ExecutorService

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

+49
java multithreading concurrency
Sep 17 '08 at 7:25
source share
6 answers

Use ThreadPoolExecutor and call getActiveCount () on it:

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

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

+55
Sep 17 '08 at 7:30
source share

Check the source code for Executors.newFixedThreadPool ():

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

ThreadPoolExecutor has a getActiveCount () method. That way you can either pass the ExecutorService to ThreadPoolExecutor, or use the above code directly to get it. Then you can call getActiveCount ().

+19
Sep 17 '08 at 7:34
source share

Assuming pool is an instance name of ExecutorService:

 if (pool instanceof ThreadPoolExecutor) { System.out.println( "Pool size is now " + ((ThreadPoolExecutor) pool).getActiveCount() ); } 
+16
Sep 01 '13 at 23:30
source share

The ExecutorService interface does not define a method for checking the number of worker threads in the pool, since this is an implementation detail

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

Available in 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 explicitly creating a ThreadPoolExecutor, and not using the Executors factory, which returns ExecutorService objects. You can always create your own factory, which returned ThreadPoolExecutors, but you will still be left with a bad form of using a particular type, rather than its interface.

One possibility is to provide your own ThreadFactory, which creates threads in a well-known thread group that you can 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 ());
     }
 }
+9
Sep 17 '08 at 7:43
source share

I had the same problem, so a simple Runnable was created to track an instance of 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 ######"); } } } 

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

Ex

 ExecutorService executorService = Executors.newFixedThreadPool(4); executorService.execute(new ExecutorServiceAnalyzer(executorService, 1000)); 
+3
Jul 04 '16 at 10:48
source share

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

-four
Sep 17 '08 at 7:31
source share



All Articles