Any ideas how to determine the number of active threads currently running in an 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.
An active java thread is a thread that is eligible to be the currently running thread of execution. It is a thread that has left the "new" state and has attained (or re-attained) the "runnable" state. Active threads take up residency in the runnable thread pool.
We use the Executors. newSingleThreadExecutor() method to create an ExecutorService that uses a single worker thread for executing tasks. If a task is submitted for execution and the thread is currently busy executing another task, then the new task will wait in a queue until the thread is free to execute it.
The Java ExecutorService is a construct that allows you to pass a task to be executed by a thread asynchronously. The executor service creates and maintains a reusable pool of threads for executing submitted tasks.
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() ); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With