Java Executor Service

Su Suriya Ravichandran Updated 18 Nov 2026
10 min read ·Lesson 193 of 306

The Java ExecutorService runs tasks on a pool of reusable threads, so you do not have to create and manage Thread objects yourself. You hand it work, and it decides which thread runs the work and when.

Creating a new thread for every small task is slow and can exhaust memory under load. An ExecutorService keeps a fixed set of worker threads, queues extra tasks, gives you results through Future objects, and shuts everything down cleanly. It lives in the java.util.concurrent package.


Why Use ExecutorService Instead of new Thread()

In Java Create Threads you started each task with new Thread(...).start(). That works for a few tasks, but it has limits.

new Thread() for each taskExecutorService
Thread creationOne new thread per taskThreads are created once and reused
Limit on threadsNone: 10,000 tasks means 10,000 threadsYou choose the pool size
Getting a resultShared variables + join()Future.get()
ExceptionsLost unless you add a handlerCaptured in the Future
Shutting downTrack and join every threadshutdown(), close(), awaitTermination()

How to Create an ExecutorService in Java

The Executors class has factory methods that create ready-made executors. The most common one is newFixedThreadPool(n), which keeps exactly n worker threads.

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

public class FirstExecutor {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(2); // two worker threads

        for (int i = 1; i <= 4; i++) {
            int orderNo = i;
            executor.execute(() ->
                System.out.println("Packing order " + orderNo + " on "
                        + Thread.currentThread().getName()));
        }

        executor.shutdown();                              // accept no new tasks
        executor.awaitTermination(5, TimeUnit.SECONDS);   // wait for queued tasks to finish
        System.out.println("All orders packed");
    }
}

Output (the order and thread assignment can change):

Packing order 1 on pool-1-thread-1
Packing order 2 on pool-1-thread-2
Packing order 3 on pool-1-thread-1
Packing order 4 on pool-1-thread-2
All orders packed

How it works:

  • newFixedThreadPool(2) creates a pool with two worker threads named pool-1-thread-1 and pool-1-thread-2.
  • Four tasks are given to execute(). Two run straight away, and two wait in the executor's queue until a worker is free.
  • shutdown() stops new tasks from being accepted but lets the queued ones finish. awaitTermination() blocks main until they are done (or 5 seconds pass).

Executors Factory Methods

Factory methodThreadsTypical use
newFixedThreadPool(n)Exactly nGeneral work with a known limit
newSingleThreadExecutor()1Tasks that must run one at a time, in order
newCachedThreadPool()Grows and shrinks as neededMany short tasks
newScheduledThreadPool(n)n, can delay or repeat tasksTimers and periodic jobs
newWorkStealingPool()One per CPU coreMany small CPU-bound tasks
newVirtualThreadPerTaskExecutor() (Java 21+)One virtual thread per taskLarge numbers of I/O-bound tasks

Note: A pool must be shut down when you are finished with it. Its worker threads are not daemon threads, so a forgotten executor keeps the JVM running after main ends.


ExecutorService execute() vs submit()

There are two ways to give a task to an executor. execute() comes from the Executor interface and returns nothing. submit() returns a Future, which lets you wait for the task, check its status, cancel it, or read its result.

execute(Runnable)submit(Runnable)submit(Callable<T>)
ReturnsvoidFuture<?>Future<T>
ResultNoneget() returns nullget() returns the value
Exception in taskPrinted by the worker threadStored in the FutureStored in the Future
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class SubmitExample {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newFixedThreadPool(2);

        // submit a Runnable: no result, but we can wait for it
        Future<?> backup = executor.submit(() -> {
            sleepQuietly(300);
            System.out.println("Backup finished");
        });

        // submit a Callable: it returns a value
        Future<Integer> userCount = executor.submit(() -> {
            sleepQuietly(200);
            return 1_250;
        });

        System.out.println("Backup done yet? " + backup.isDone());
        System.out.println("Users: " + userCount.get()); // waits for the result
        backup.get();                                     // waits, returns null
        System.out.println("Backup done now? " + backup.isDone());

        executor.shutdown();
    }

    static void sleepQuietly(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

Output:

Backup done yet? false
Users: 1250
Backup finished
Backup done now? true

How it works:

  • The first lambda returns nothing, so it is a Runnable. The second returns a value, so Java treats it as a Callable<Integer>.
  • Both tasks start at once on two different workers. isDone() is false immediately after submitting.
  • userCount.get() blocks main for about 200 ms until the result is ready. backup.get() then blocks until the backup finishes.
  • Callables and Future are covered in depth in Java Callable Interface.

Shutting Down an ExecutorService

An executor has three shutdown methods. Picking the right one decides whether queued work finishes or is thrown away.

MethodNew tasksQueued tasksRunning tasksBlocks?
shutdown()RejectedStill runFinish normallyNo
shutdownNow()RejectedRemoved and returned as a listInterruptedNo
awaitTermination(time, unit)–––Yes, until done or timeout
close() (Java 19+)RejectedStill runFinish normallyYes, until all done
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ShutdownNowExample {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newSingleThreadExecutor();

        for (int i = 1; i <= 5; i++) {
            int taskNo = i;
            executor.execute(() -> {
                System.out.println("task " + taskNo + " started");
                try {
                    Thread.sleep(1000);
                    System.out.println("task " + taskNo + " finished");
                } catch (InterruptedException e) {
                    System.out.println("task " + taskNo + " interrupted");
                }
            });
        }

        Thread.sleep(300);                               // let task 1 start
        List<Runnable> neverStarted = executor.shutdownNow();
        boolean terminated = executor.awaitTermination(1, TimeUnit.SECONDS);

        System.out.println("tasks never started: " + neverStarted.size());
        System.out.println("terminated: " + terminated);
    }
}

Output:

task 1 started
task 1 interrupted
tasks never started: 4
terminated: true

How it works:

  • A single-thread executor runs task 1 while tasks 2–5 wait in the queue.
  • shutdownNow() interrupts the running task, which wakes from sleep() with an InterruptedException. Tasks 2–5 are removed from the queue and returned as a list.
  • awaitTermination() waits for the worker to stop, so "task 1 interrupted" is printed before main prints the totals.

Using try-with-resources (Java 19+)

Since Java 19, ExecutorService implements AutoCloseable. Its close() method calls shutdown() and then waits for all tasks to finish, so a try-with-resources block replaces the manual shutdown code.

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

public class TryWithResourcesExecutor {
    public static void main(String[] args) {
        try (ExecutorService executor = Executors.newFixedThreadPool(3)) {
            for (String city : new String[] {"Chennai", "Madurai", "Coimbatore"}) {
                executor.submit(() -> System.out.println("Weather updated for " + city));
            }
        } // close(): no new tasks, then wait until every task is finished

        System.out.println("All cities updated");
    }
}

Output (the city order can change):

Weather updated for Chennai
Weather updated for Coimbatore
Weather updated for Madurai
All cities updated

How it works:

  • The executor is created in the try (...) header, so Java calls close() automatically when the block ends.
  • close() waits for all three tasks, so "All cities updated" is always printed last.
  • This is the cleanest pattern on modern Java. On Java 17 or older, use shutdown() plus awaitTermination() in a finally block.

Running Many Tasks with invokeAll and invokeAny

invokeAll() runs a whole collection of Callable tasks and waits until all of them finish. It returns a list of Futures in the same order as the tasks. invokeAny() waits only for the first task that succeeds, returns its result, and cancels the rest.

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class InvokeAllAny {
    static Callable<String> download(String mirror, long delayMs) {
        return () -> {
            Thread.sleep(delayMs);           // simulate network time
            return mirror;
        };
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        List<Callable<String>> mirrors = List.of(
                download("mirror-mumbai", 300),
                download("mirror-chennai", 100),
                download("mirror-delhi", 200));

        try (ExecutorService executor = Executors.newFixedThreadPool(3)) {
            List<Future<String>> all = executor.invokeAll(mirrors); // waits for all three
            for (Future<String> result : all) {
                System.out.println("invokeAll result: " + result.get());
            }

            String fastest = executor.invokeAny(mirrors);           // first one to succeed
            System.out.println("invokeAny result: " + fastest);
        }
    }
}

Output:

invokeAll result: mirror-mumbai
invokeAll result: mirror-chennai
invokeAll result: mirror-delhi
invokeAny result: mirror-chennai

How it works:

  • invokeAll() returns only when all three downloads are complete (after about 300 ms). The results keep the order of the input list, not the order the tasks finished in.
  • invokeAny() returns as soon as the 100 ms Chennai mirror finishes and cancels the other two.
  • Both methods also have versions with a timeout, for example invokeAll(tasks, 2, TimeUnit.SECONDS).

Handling Exceptions in ExecutorService Tasks

If a task throws an exception, what you see depends on how it was started. With execute(), the worker thread prints the stack trace and is replaced by a new worker. With submit(), the exception is stored in the Future and only appears when you call get(), wrapped in an ExecutionException.

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TaskExceptions {
    public static void main(String[] args) throws InterruptedException {
        try (ExecutorService executor = Executors.newSingleThreadExecutor()) {
            Future<Integer> quantity = executor.submit(() -> Integer.parseInt("12a"));

            try {
                System.out.println("Quantity: " + quantity.get());
            } catch (ExecutionException e) {
                System.out.println("Task failed: " + e.getCause());
            }
        }
    }
}

Output:

Task failed: java.lang.NumberFormatException: For input string: "12a"

How it works:

  • Integer.parseInt("12a") throws a NumberFormatException inside the worker thread.
  • Because the task was submitted, nothing is printed at that moment. The exception waits in the Future.
  • get() throws an ExecutionException, and getCause() returns the original exception.

Note: If you submit() a task and never call get(), its exception is silently lost. Either call get(), or catch and log exceptions inside the task itself.


Real-World Example: Generating Order Reports in Parallel

An online shop must build reports for six orders. Each report needs a slow database call of about 200 ms. Run one after another, that takes 1.2 seconds; with a pool of three workers, it takes about 400 ms.

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class OrderReportGenerator {
    record OrderReport(String orderId, int items, long totalPaise, String builtBy) { }

    // Simulates loading an order from a database and building its report
    static OrderReport buildReport(String orderId) throws InterruptedException {
        Thread.sleep(200);                                   // slow database call
        int items = orderId.length() % 4 + 1;
        long totalPaise = items * 49_900L;
        return new OrderReport(orderId, items, totalPaise, Thread.currentThread().getName());
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        List<String> orderIds = List.of("ORD-101", "ORD-1022", "ORD-10333",
                                        "ORD-104", "ORD-1055", "ORD-10666");

        List<Callable<OrderReport>> jobs = new ArrayList<>();
        for (String id : orderIds) {
            jobs.add(() -> buildReport(id));
        }

        long start = System.currentTimeMillis();
        long grandTotalPaise = 0;

        try (ExecutorService reportPool = Executors.newFixedThreadPool(3)) {
            List<Future<OrderReport>> results = reportPool.invokeAll(jobs);
            for (Future<OrderReport> future : results) {
                OrderReport report = future.get();
                grandTotalPaise += report.totalPaise();
                System.out.printf("%-9s items=%d total=Rs. %8.2f (%s)%n",
                        report.orderId(), report.items(), report.totalPaise() / 100.0, report.builtBy());
            }
        }

        long elapsed = System.currentTimeMillis() - start;
        System.out.printf("Grand total: Rs. %.2f%n", grandTotalPaise / 100.0);
        System.out.println("Took about " + Math.round(elapsed / 100.0) * 100 + " ms (sequential: 1200 ms)");
    }
}

Output (thread names and exact timing can vary):

ORD-101   items=4 total=Rs.  1996.00 (pool-1-thread-1)
ORD-1022  items=1 total=Rs.   499.00 (pool-1-thread-2)
ORD-10333 items=2 total=Rs.   998.00 (pool-1-thread-3)
ORD-104   items=4 total=Rs.  1996.00 (pool-1-thread-1)
ORD-1055  items=1 total=Rs.   499.00 (pool-1-thread-3)
ORD-10666 items=2 total=Rs.   998.00 (pool-1-thread-2)
Grand total: Rs. 6986.00
Took about 400 ms (sequential: 1200 ms)

How it works:

  1. One task per order. Each Callable calls buildReport() for one order ID and returns an OrderReport record.
  2. Parallel execution. invokeAll() hands the six jobs to three workers. Three reports are built in the first 200 ms and three in the next 200 ms.
  3. Ordered results. The list of Futures matches the order of orderIds, so the printout is in order even though the work ran in parallel.
  4. Automatic cleanup. The try-with-resources block shuts the pool down and waits for it, so no worker threads are left running.

The next lesson, Java Thread Pools, looks inside these executors: how ThreadPoolExecutor decides when to create threads, queue tasks or reject them. For the basics of the tasks themselves, see Java Runnable Interface.

0 Comments

Reviewed before they appear

No comments yet.

Java
Java Introduction Java Features Java Syntax Java Comments Java Statements Java Output Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Numbers Java Math Java Booleans Java User Input Java Conditions Java If Else Java Switch Java While Loop Java For Loop Java Break Java Continue Java Arrays Java Methods Java Parameters Java Return Values Java Scope Java Recursion Install Java Install JDK Java IDEs Java JDK Java JRE Java JVM Java Compiler Java File Compile Java Run Java JShell Basics Java Packages Java OOP Java Classes Java Objects Class Methods Constructors Constructor Overload this Keyword static Keyword final Keyword Encapsulation Inheritance Polymorphism Abstraction Access Modifiers super Keyword Method Overload Method Override Instanceof Operator Nested Classes Inner Classes Anonymous Classes Enums Records Array Basics Array Length Loop Arrays Multidimensional Arrays Array Copy Array Sorting Array Search ArrayList LinkedList HashSet TreeSet HashMap TreeMap Queue Interface Deque Interface Stack Class Iterator List Interface Set Interface Map Interface Collections Class Comparable Comparator Generics Basics Generic Classes Generic Methods Wildcards String Basics String Length String Methods String Concatenation String Compare String Search String Replace String Split String Format StringBuilder StringBuffer Text Blocks Regular Expressions Number Types Integer Class Float Class Double Class BigInteger BigDecimal Math Class Random Numbers Number Format Date Basics LocalDate LocalTime LocalDateTime ZonedDateTime Date Formatting Date Parsing Period Class Duration Class Java Instant Class Java Exception Basics Java try...catch Java Multiple Catch Blocks Java finally Block Java throw Keyword Java throws Keyword Java Checked Exceptions Java Unchecked Exceptions Java Custom Exceptions Java try-with-resources Java Exception Chaining Java File Basics Java Create Files Java Read Files Java Write Files Java Delete Files Java File Paths Java File Class Java BufferedReader Java BufferedWriter Java FileInputStream Java FileOutputStream Java Object Streams Java Directory Handling Java NIO Basics Java Files API Java Input Java Scanner Class Java Console Input Java PrintStream Java InputStream Java OutputStream Java Reader Class Java Writer Class Java Buffered Streams Java Data Streams Java Generics Introduction Java Generic Types Java Generic Methods Java Bounded Types Java Wildcards Java Type Erasure Java Generic Interfaces Java Generic Collections Java Lambda Basics Java Lambda Syntax Java Functional Interfaces Java Method References Java Stream API Java Stream Creation Java Stream Filter Java Stream Map Java Stream Reduce Java Stream Sort Java Stream Collect Java Collectors Class Java Optional Class Java Parallel Streams Java Threads: Thread Basics Create a Thread in Java Java Thread Class Java Runnable Interface Java Thread States Java Thread Sleep Java Thread Join Java Thread Priority Java Synchronization Java Thread Safety Java Race Conditions Java Deadlocks Java Executor Service Java Thread Pools Java Callable Interface Java Future Interface Java CompletableFuture Java Concurrent Collections Java Atomic Classes Java Virtual Threads Java Networking Basics Java URL Class Java URI Class Java HttpClient Java HTTP Requests Java HTTP Responses Java Socket Basics Java ServerSocket Java DatagramSocket Java WebSocket Basics Java JDBC Basics Java JDBC Setup Java Database Connection Java JDBC Connection Interface Java JDBC Statement Java PreparedStatement Java ResultSet Java JDBC CRUD Operations Java JDBC Transactions Java JDBC Batch Update Java Connection Pooling with HikariCP Java JPA Basics: Entities, EntityManager and JPQL Java Hibernate Basics: SessionFactory, Session and HQL Package in Java: Basics Explained with Examples Create Package in Java: Step-by-Step Guide with Examples Import Package in Java: The import Statement Explained Access Control in Java: Packages, Classes and Members JAR File in Java: Create, Run and Inspect JAR Files Java Modules: Introduction to the Java Module System module-info in Java: Every Module Directive Explained Java Module Path: How --module-path Finds Your Modules Java Classpath: How -cp and CLASSPATH Find Your Classes Java Annotations: Basics of @ Metadata in Java Java Built-in Annotations: @Override, @Deprecated and More Java Custom Annotations: Create Your Own @interface Java Annotation Targets: @Target and ElementType Explained Java Reflection API: Inspect and Use Classes at Runtime Java Class Metadata: Names, Modifiers, Hierarchy and Generic Java Dynamic Objects: Create Objects Dynamically With Reflec Java Dynamic Proxy: Intercept Method Calls With Proxy and In Java Security Java Secure Coding Java Password Hashing Java Input Validation Java Cryptography Basics Java KeyStore Basics Java TLS Basics Java SecureRandom Java JUnit Basics Java JUnit Test Methods JUnit Assertions JUnit Test Fixtures JUnit Parameterized Tests Mockito in Java Java Mock Objects Java Integration Testing Java Code Coverage Maven in Java Create a Maven Project in Java Maven Dependencies Maven Plugins Gradle in Java Gradle Build Gradle Dependencies Build Executable JAR in Java Build WAR File in Java HTTP Basics in Java Java Servlet Basics Servlet Lifecycle in Java HttpServletRequest in Java HttpServletResponse in Java JSP in Java JSTL in JSP REST API in Java JSON in Java Java Web Application Spring Framework Basics Spring Boot Tutorial Spring MVC Spring Data JPA Spring Security Spring Boot REST API Jakarta EE Basics Java Deployment JAR Deployment Java WAR Deployment Run Java as a Service Docker Java: Dockerize a Java Application Java Environment Variables and Configuration Java Logging Basics Java Production Setup Java JVM Options Java JVM Monitoring Java Hello World App Java Calculator App Java Number Guess Game Java Student Manager Java Library System Java Bank App Java Todo App Java File Manager Java Quiz App Java REST API Project Java Chat Server Java Employee Manager Java E-commerce API
Ask about this post
AI Ask about this post

Ask questions about Java Executor Service and get answers drawn from it.

Signed-in readers only.