Active threads in ExecutorService
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.
Assuming pool
is the name of the ExecutorService instance:
if (pool instanceof ThreadPoolExecutor) {
System.out.println(
"Pool size is now " +
((ThreadPoolExecutor) pool).getActiveCount()
);
}