Active threads in ExecutorService

JavaMultithreadingConcurrency

Java Problem Overview


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

Java Solutions


Solution 1 - Java

Use a http://java.sun.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html">ThreadPoolExecutor</a> implementation and call http://java.sun.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html#getActiveCount()">getActiveCount()</a> 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.

Solution 2 - Java

Assuming pool is the name of the ExecutorService instance:

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

Solution 3 - Java

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().

Solution 4 - Java

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

}

Solution 5 - Java

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

Solution 6 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzanussiView Question on Stackoverflow
Solution 1 - JavaDaanView Answer on Stackoverflow
Solution 2 - JavaandyroidView Answer on Stackoverflow
Solution 3 - JavaArnoView Answer on Stackoverflow
Solution 4 - JavaDave CheneyView Answer on Stackoverflow
Solution 5 - JavaAnkit KatiyarView Answer on Stackoverflow
Solution 6 - JavaJavaxpertView Answer on Stackoverflow