The thread pool in Java is responsible for the provision of threads to perform the given tasks. The threads are returned back to the thread pool after the task has ended. New threads are created or the previously created threads are used for the execution of tasks. If the task has not been submitted to the thread pool due to any reason it is therefore managed by the RejectedExecutionHandler.
This article will elaborate in detail on the working of the getActiveCount() method of ThreadPoolExecutor in Java.
How to Use ThreadPoolExecutor getActiveCount() Method of Java?
The getActiveCount() of ThreadPoolExecutor in Java is responsible for providing the number of threads that are executing the task actively. In simple terms, the number of threads that are basically executing the provided task is obtained through the getActiveCount() method.
Below is the syntax of the getActiveCount() method:
Public int getActiveCount()
Example: The Implementation of the getActiveCount() Method
The code implementation of the getActiveCount() method that fetches the number of active threads is depicted below:
import java.util.concurrent.*;
import java.lang.*;// The Class declared for the activecount method.
class Activecountexample {
public static void main(final String[] arguments) throws InterruptedException {
// The newcachedThreadpool for the creation of new threads or reuse of the previously created.
ThreadPoolExecutor executingthread = (ThreadPoolExecutor) Executors.newCachedThreadPool();
// The getActiveCount() method to get the number of threads active.
System.out.println("The Threads that are currently executing are " + executingthread.getActiveCount());
// First task has been submitted for execution
executingthread.submit(new newtask());
//The second task has been submitted for execution
executingthread.submit(new newtask());
// Third task has been submitted for execution
executingthread.submit(new newtask());
// the shutdown() method to end the tasks.
executingthread.shutdown();
}
// The Runnable interface is implemented by the run() method static class newtask implements Runnable {
@Override
public void run() {
try {
Long timespan = (long) (Math.random() * 6);
// The threads that are currently executing the tasks.
System.out.println("The name of thread that is currently running: " + Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(timespan);
// The threads that have finished executing the tasks.
System.out
.println("The name of thread that has completed running: " + Thread.currentThread().getName());
// The exception in the catch block
} catch (InterruptedException f) {
}
}
}
}
In this Java code:
- The required packages are imported.
- In order to create new threads or reuse the ones that have been created previously the newCached() method is used.
- The getActivecount() fetches the number of threads that are running currently.
- Three new tasks are submitted for execution.
- After the execution, these tasks are shut down using the shutdown() method.
- The newtask is implemented using the Runnable interface.
- The run() method of the Runnable interface contains the timespan for which the threads are made to sleep
- The println() method of Java prints the currently running threads, and threads that have completed running.
Output
In the output below, the currently executing threads are 0. Each thread runs in its specified order, that is thread 3 runs first then thread 2 and at the end thread 1 runs. The completed threads are printed in a sequence of 3, 2, and 1. This is the output in our case when the getActiveCount() method is implemented. The currently running and completed threads usually appear in random order and may differ from the below output.

This ends the working of the getActiveCount() method of ThreadPoolExecutor in Java.
Conclusion
In Java, the getActiveCount() function of ThreadPoolExecutor is responsible for providing the number of threads that are executing the task actively. This article describes the working and implementation of the getActiveCount() method through code examples in Java.