Executor & Executor Service

Executor and ExecutorService are the utility classes present starting from JDK 1.5 for executing a java process/block parallely in a controlled manner.

Executor – Executor is used for creates a thread pool to for fixed number, using the method newFixedThreadPool.

e.g., We have a 2 dimensional array where each row needs to be processed or set to particular String.

We are instantiating the ThreadPool by 2 or 3 by using the ExecutorService.

Each Thread will execute the run method, which will process the corresponding row of the 2D array.

Below statement, initiates an orderly shutdown in which previously submitted tasks are executed, and no new tasks.Invocation has no additional effect if already shut down.

Make the executor wait for Maximum of 10 seconds, if all threads are not processed, give an interruption.


The below program compares the time spent on the 2D array processing via Executor and normal for loop.

In the below example, for approximately 200 iterations, the time spent in milliseconds doesn’t make any difference.
However for greater than 200, the time saved by Executor via parallel processing makes significant difference, since multiple threads

Since multiple threads are used, Java Executors are faster and effective the for loop for larger sets of iterations., considering memory of the heap.

Example Below:


Leave a Reply

Your email address will not be published. Required fields are marked *