The thread pool executor class in Java belongs to the executor service in Java that is basically responsible for the execution of the tasks submitted. Various methods in the ThreadPoolExecutor class have been helpful in determining the current state of tasks, the pool size, the threads that have been actively running, and many more.
In this write-up, the getCorePool size() method of ThreadPoolExecutor in Java will be discussed in detail.
Thread Pool Executor getCorePoolSize() Method in Java
The getCorePoolSize() method has the responsibility of calculating the number of threads that are running the tasks currently. In simple words, the threads that are alive in the pool are considered as the core pool size.
getPoolSize() Syntax
The syntax of the method is illustrated below.
Public int getCorePoolSize()
Example: Implementation of getCorePoolSize() method in Java
The code below will depict the implementation of the getPoolSize() method:
//Import all the packages of concurrent
import java.util.concurrent.*;
import java.lang.Thread;
// Declare a class
class corepoolsize {
//Interrupted Exception
public static void main(final String[] arguments) throws InterruptedException {
ThreadPoolExecutor execute = (ThreadPoolExecutor)Executors.newCachedThreadPool();
// Submit() method to get the return value
execute.submit(new nTasks());
execute.submit(new nTasks());
//Get the core pool size execution time
System.out.println("CORE POOL SIZE METHOD");
System.out.println("The size of Core pool after execution is: " + execute.getCorePoolSize());
//After the process is complete shutdown
execute.shutdown();
}
//the runnable interface for the thread to run
static class nTasks implements Runnable {
@Override
//The try catch block in the run method for the running threads
public void run() {
try {
Long duration = (long) (Math.random() *7 );
//Output for the threads currently running
System.out.println("Current thread that is running: " + Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(duration);
//Output for the threads that have completed running
System.out.println("The thread that has completed running: " +Thread.currentThread().getName());
} catch (InterruptedException e) { }
}
}
}
In the above code:
- The respective packages have been imported.
- A class has been created and an Interrupted exception has been passed.
- The cached thread pool will create new threads when needed otherwise it will reuse the previously created threads.
- The tasks have been submitted for execution.
- The getCorePoolSize() method will return the size of the core pool after the execution.
- The shutdown() method will shut down the currently running tasks.
- The run() method of the Runnable interface has the thread that is currently running and the thread that has completed running.
Output

In the output above:
- The threads from the respective pools have been printed.
- The size of CorePool after the execution is 0.
- The threads that have now completed running have been printed accordingly.
This brings the end to this write-up on the getCorePoolSize() method in Java.
Conclusion
The thread pool executor method in Java consists of multiple functions. One of the main functions is the getCorePoolSize() which is basically responsible for delivering the number of threads that have been currently running the tasks. In this article, we have explained in detail the implementation of the getCorePoolSize method of Java from the ThreadPoolExecutor class.