Java Programming Tutorials

Java programming tutorials with many code examples!

Java ExecutorService example


How to execute threads using Java ExecutorService? Since Java 5 it’s the preferred way to run Threads. In the following example we’ll show how to do that.


In previous posts (for example Java Thread Example) we’ve shown how to run tasks in separate threads manually. Well, that was cool 15 years ago :-) and in Java 5 have been replaced with java.util.concurrent that makes Thread related work much easier.

Here we’ll show Java ExecutorService example, which executes a couple of tasks at the same time. We’ll only implement a Runnable task and allow the executor to handle thread creation, starting, stopping, and reuse if needed (think how much you would need to implement to provide such functionality!):


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ExecutorServiceExample {

    private static class Counter implements Runnable {

        private static int nth = 0;

        private final int id = ++nth;
        private final int number;

        public Counter(int number) {
            this.number = number;

        public void run() {
            for (int i = 0; i < number; ++i) {
                try {
                } catch (InterruptedException e) {
                    // ignore
                System.out.printf("counter %d, value: %d%n", id, i);

    public static void main(String&#91;&#93; args) {
        System.out.println("Starting ExecutorService");
        ExecutorService executor = Executors.newSingleThreadExecutor();
        for (int i = 1; i <= 5; ++i) {
            executor.execute(new Counter(i));

        System.out.println("Shutting down the executor");
        // Comment out to hang the app:

        //List<Runnable> notStarted = executor.shutdownNow();
        //System.out.println("Number of not started: " + notStarted.size());

        System.out.println("The main thread ends now.");

In the above code we used Single Thread Pool Executor to execute tasks sequentially, which doesn’t matter much here and we’ll describe it in further posts.

What matters though is how to execute tasks using execute(Runnable) method and stopping the executor using shutdown() method. As an exercise run the example, but with executor.shutdown() commented out. :-)

Here’s the result of running the above program:

Starting ExecutorService
Shutting down the executor
The main thread ends now.
counter 1, value: 0
counter 2, value: 0
counter 2, value: 1
counter 3, value: 0
counter 3, value: 1
counter 3, value: 2
counter 4, value: 0
counter 4, value: 1
counter 4, value: 2
counter 4, value: 3
counter 5, value: 0
counter 5, value: 1
counter 5, value: 2
counter 5, value: 3
counter 5, value: 4

As you can see the program waits for already submitted tasks to finish their work, even though the shutdown() method has been called. If you don’t want to wait for the tasks to finish you can call shutdownNow() method, which will try to stop the tasks immediately.

Here the result of running executor.shutdownNow():

Starting ExecutorService
Shutting down the executor
Number of not started: 4
The main thread ends now.
counter 1, value: 0

Shorter, no? :-)

What is also important here, the ExecutorService is created using static Factory method, which we can change to create executors with different characteristics, as we’ll show in the following posts. So stay tuned! :-)

Was it helpful? Share!