Back to Thread Pool & Executor Framework
Q1Question 1 of 1

How do Executors, thread pools, and CompletableFuture work in Java

Answer: The Executor framework manages a pool of reusable threads that execute submitted tasks, and CompletableFuture builds async pipelines by chaining non-blocking operations like thenApply, thenCompose, and thenCombine.

Key Concepts
  • The Executor framework decouples task submission from thread creation, letting you submit Runnable or Callable tasks to a pool of reusable threads.
  • Thread pools avoid the cost of creating and destroying threads for every task by keeping a fixed number of threads alive and reusing them.
  • Java provides four main thread pool types: newFixedThreadPool (fixed size), newCachedThreadPool (grows on demand), newSingleThreadExecutor (one thread), and newScheduledThreadPool (delayed and periodic tasks).
  • CompletableFuture is an async programming tool that lets you chain operations (thenApply, thenCompose, thenCombine) without blocking the calling thread.
  • Virtual thread executors (Executors.newVirtualThreadPerTaskExecutor) create one virtual thread per task, ideal for IO-bound workloads with thousands of concurrent tasks.
  • Always shut down an ExecutorService with shutdown() or use try-with-resources (Java 19+) to prevent thread leaks.
  • CompletableFuture.allOf() and anyOf() let you run multiple async tasks in parallel and wait for all or the fastest to complete.

Definition

The Executor framework manages a pool of reusable threads that execute submitted tasks, and CompletableFuture builds async pipelines by chaining non-blocking operations like thenApply, thenCompose, and thenCombine.
  • Executor is the base interface with a single execute(Runnable) method that decouples task submission from how tasks are run.
  • ExecutorService extends Executor with lifecycle management (shutdown(), awaitTermination()) and the ability to submit Callable tasks that return Future results.
  • Thread pool is a group of pre-created worker threads that pick tasks from a queue and execute them, avoiding the overhead of creating a new thread for every task.
  • CompletableFuture is a Future that you can complete manually and chain with callbacks (thenApply, thenCompose, thenCombine, exceptionally) to build non-blocking async workflows.
  • Virtual thread executor (Java 21+) is a special executor that creates one virtual thread per submitted task, allowing millions of concurrent tasks for IO-bound workloads without the thread-sizing problem of fixed thread pools.

Think of the loan processing department at National Bank of India. Without a thread pool, the bank hires a new clerk for every loan application and fires them when done. With a thread pool (ExecutorService), the bank keeps a team of 10 clerks: loan applications go into a queue, and the next available clerk picks one up. CompletableFuture is like the loan pipeline: step 1 checks the credit score (async), step 2 verifies income (async), and when both complete, step 3 calculates the loan amount. Each step hands off to the next without the branch manager standing there waiting. The virtual thread executor is like hiring a temporary intern for every single micro-task: each intern costs almost nothing, so you can have thousands of them handling different parts of the loan process simultaneously.

Understanding the Concepts

What is the Executor framework?

The Executor framework is Java's standard way to run tasks asynchronously without manually creating threads. Instead of writing new Thread(task).start() for every task, you submit the task to an ExecutorService, which manages a pool of threads and assigns tasks to available workers.

  • The framework has three main interfaces:
  • Executor: the simplest, has only execute(Runnable).
  • ExecutorService: adds submit() (returns a Future), shutdown(), and awaitTermination().
  • ScheduledExecutorService: adds schedule(), scheduleAtFixedRate(), and scheduleWithFixedDelay().
JAVA
package com.bank.executor;

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

public class ExecutorBasicsDemo {
    public static void main(String[] args) throws InterruptedException {
        // Create a thread pool with 3 worker threads
        ExecutorService executor = Executors.newFixedThreadPool(3);

        System.out.println("Submitting 5 banking tasks to a 3-thread pool:");
        System.out.println();

        for (int i = 1; i <= 5; i++) {
            final int taskId = i;
            final String accountId = "SAV200" + taskId;
            executor.submit(() -> {
                System.out.println("  [" + Thread.currentThread().getName()
                    + "] Processing transaction for " + accountId);
                try {
                    Thread.sleep(200); // Simulate database call
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                System.out.println("  [" + Thread.currentThread().getName()
                    + "] Completed " + accountId);
            });
        }

        executor.shutdown();
        executor.awaitTermination(10, TimeUnit.SECONDS);

        System.out.println();
        System.out.println("All 5 tasks completed with only 3 threads.");
        System.out.println("Tasks 4 and 5 waited in the queue until a thread was free.");
    }
}
TEXT
Expected output:
Submitting 5 banking tasks to a 3-thread pool:

  [pool-1-thread-1] Processing transaction for SAV2001
  [pool-1-thread-2] Processing transaction for SAV2002
  [pool-1-thread-3] Processing transaction for SAV2003
  [pool-1-thread-1] Completed SAV2001
  [pool-1-thread-1] Processing transaction for SAV2004
  [pool-1-thread-2] Completed SAV2002
  [pool-1-thread-2] Processing transaction for SAV2005
  [pool-1-thread-3] Completed SAV2003
  [pool-1-thread-1] Completed SAV2004
  [pool-1-thread-2] Completed SAV2005

All 5 tasks completed with only 3 threads.
Tasks 4 and 5 waited in the queue until a thread was free.

What types of thread pools does Java provide?

The Executors factory class provides four standard thread pool types, plus the virtual thread executor (Java 21+).

Thread PoolThreadsQueueBest For
newFixedThreadPool(n)Fixed at nUnbounded LinkedBlockingQueueKnown, steady workload (batch processing)
newCachedThreadPool()0 to Integer.MAX_VALUE, idle threads expire in 60sSynchronousQueue (no capacity)Short-lived burst tasks
newSingleThreadExecutor()Exactly 1Unbounded LinkedBlockingQueueSequential task execution (event log)
newScheduledThreadPool(n)Fixed core of nDelayedWorkQueuePeriodic tasks (report generation, cleanup)
newVirtualThreadPerTaskExecutor()One virtual thread per taskNo queue (immediate dispatch)IO-bound tasks (database, HTTP calls)
JAVA
package com.bank.executor;

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

public class ThreadPoolTypesDemo {
    public static void main(String[] args) throws InterruptedException {
        // 1. FixedThreadPool: predictable resource usage
        System.out.println("=== FixedThreadPool (3 threads) ===");
        try (ExecutorService fixed = Executors.newFixedThreadPool(3)) {
            for (int i = 0; i < 3; i++) {
                fixed.submit(() -> System.out.println("  Fixed: " + Thread.currentThread().getName()));
            }
            fixed.shutdown();
            fixed.awaitTermination(5, TimeUnit.SECONDS);
        }

        // 2. CachedThreadPool: grows on demand
        System.out.println();
        System.out.println("=== CachedThreadPool ===");
        try (ExecutorService cached = Executors.newCachedThreadPool()) {
            for (int i = 0; i < 5; i++) {
                cached.submit(() -> System.out.println("  Cached: " + Thread.currentThread().getName()));
            }
            cached.shutdown();
            cached.awaitTermination(5, TimeUnit.SECONDS);
        }

        // 3. SingleThreadExecutor: sequential
        System.out.println();
        System.out.println("=== SingleThreadExecutor ===");
        try (ExecutorService single = Executors.newSingleThreadExecutor()) {
            for (int i = 1; i <= 3; i++) {
                final int txn = i;
                single.submit(() -> System.out.println("  Single: TXN-" + txn
                    + " on " + Thread.currentThread().getName()));
            }
            single.shutdown();
            single.awaitTermination(5, TimeUnit.SECONDS);
        }

        // 4. ScheduledThreadPool: periodic tasks
        System.out.println();
        System.out.println("=== ScheduledThreadPool ===");
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleAtFixedRate(
            () -> System.out.println("  Scheduled: interest calculation tick"),
            0, 100, TimeUnit.MILLISECONDS);
        Thread.sleep(350);
        scheduler.shutdown();
        scheduler.awaitTermination(5, TimeUnit.SECONDS);

        // 5. VirtualThreadPerTaskExecutor (Java 21+)
        System.out.println();
        System.out.println("=== VirtualThreadPerTaskExecutor ===");
        try (ExecutorService virtual = Executors.newVirtualThreadPerTaskExecutor()) {
            for (int i = 0; i < 3; i++) {
                virtual.submit(() -> System.out.println("  Virtual: " + Thread.currentThread()));
            }
            virtual.shutdown();
            virtual.awaitTermination(5, TimeUnit.SECONDS);
        }
    }
}
TEXT
Expected output:
=== FixedThreadPool (3 threads) ===
  Fixed: pool-1-thread-1
  Fixed: pool-1-thread-2
  Fixed: pool-1-thread-3

=== CachedThreadPool ===
  Cached: pool-2-thread-1
  Cached: pool-2-thread-2
  Cached: pool-2-thread-3
  Cached: pool-2-thread-4
  Cached: pool-2-thread-5

=== SingleThreadExecutor ===
  Single: TXN-1 on pool-3-thread-1
  Single: TXN-2 on pool-3-thread-1
  Single: TXN-3 on pool-3-thread-1

=== ScheduledThreadPool ===
  Scheduled: interest calculation tick
  Scheduled: interest calculation tick
  Scheduled: interest calculation tick
  Scheduled: interest calculation tick

=== VirtualThreadPerTaskExecutor ===
  Virtual: VirtualThread[#21]/runnable@ForkJoinPool-1-worker-1
  Virtual: VirtualThread[#22]/runnable@ForkJoinPool-1-worker-1
  Virtual: VirtualThread[#23]/runnable@ForkJoinPool-1-worker-2

Notice that FixedThreadPool reuses the same 3 threads, CachedThreadPool created 5 threads (one per task), and SingleThreadExecutor ran all tasks sequentially on one thread.

How is newFixedThreadPool different from newCachedThreadPool?

FeaturenewFixedThreadPool(n)newCachedThreadPool()
Thread countAlways n threads0 to Integer.MAX_VALUE
Idle threadsStay alive foreverExpire after 60 seconds
QueueUnbounded LinkedBlockingQueueSynchronousQueue (zero capacity)
Overflow behaviorTasks queue up (no new threads created)New threads created immediately
RiskQueue grows unbounded if tasks arrive faster than processing (OutOfMemoryError)Thread count grows unbounded under sustained load (OutOfMemoryError)
Use caseSteady, predictable workloadShort burst of many small tasks
JAVA
package com.bank.executor;

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

public class FixedVsCachedDemo {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("=== FixedThreadPool(2): tasks queue when busy ===");
        try (ExecutorService fixed = Executors.newFixedThreadPool(2)) {
            for (int i = 1; i <= 4; i++) {
                final int task = i;
                fixed.submit(() -> {
                    System.out.println("  Task " + task + " on " + Thread.currentThread().getName());
                    try { Thread.sleep(100); } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                });
            }
            fixed.shutdown();
            fixed.awaitTermination(5, TimeUnit.SECONDS);
        }
        System.out.println("  (Only 2 threads used. Tasks 3 and 4 waited in the queue.)");

        System.out.println();
        System.out.println("=== CachedThreadPool: new thread for each task ===");
        try (ExecutorService cached = Executors.newCachedThreadPool()) {
            for (int i = 1; i <= 4; i++) {
                final int task = i;
                cached.submit(() -> {
                    System.out.println("  Task " + task + " on " + Thread.currentThread().getName());
                    try { Thread.sleep(100); } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                });
            }
            cached.shutdown();
            cached.awaitTermination(5, TimeUnit.SECONDS);
        }
        System.out.println("  (4 threads created. Each task got its own thread immediately.)");
    }
}
TEXT
Expected output:
=== FixedThreadPool(2): tasks queue when busy ===
  Task 1 on pool-1-thread-1
  Task 2 on pool-1-thread-2
  Task 3 on pool-1-thread-1
  Task 4 on pool-1-thread-2
  (Only 2 threads used. Tasks 3 and 4 waited in the queue.)

=== CachedThreadPool: new thread for each task ===
  Task 1 on pool-2-thread-1
  Task 2 on pool-2-thread-2
  Task 3 on pool-2-thread-3
  Task 4 on pool-2-thread-4
  (4 threads created. Each task got its own thread immediately.)

What is CompletableFuture?

CompletableFuture is an async programming tool introduced in Java 8. Unlike Future, which blocks when you call get(), CompletableFuture lets you chain callbacks that run when the result is available. You build a pipeline of steps: "when step 1 completes, run step 2 on the result, then step 3, then handle any exception."

  • Key methods:
  • supplyAsync(Supplier): starts an async task that returns a value.
  • thenApply(Function): transforms the result (like map on a stream).
  • thenCompose(Function): chains another CompletableFuture (like flatMap).
  • thenCombine(CompletableFuture, BiFunction): combines two results.
  • exceptionally(Function): handles exceptions.
  • thenAccept(Consumer): consumes the result without returning a new value.
JAVA
package com.bank.executor;

import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;

public class CompletableFutureBasicsDemo {
    public static void main(String[] args) {
        System.out.println("=== CompletableFuture Chain ===");
        System.out.println("Processing loan application for Rajesh (SAV2001)");
        System.out.println();

        CompletableFuture<String> pipeline = CompletableFuture
            // Step 1: Fetch credit score (async)
            .supplyAsync(() -> {
                System.out.println("  [" + Thread.currentThread().getName()
                    + "] Step 1: Fetching credit score for SAV2001");
                sleep(100);
                return 750; // credit score
            })
            // Step 2: Determine loan eligibility (transform result)
            .thenApply(creditScore -> {
                System.out.println("  [" + Thread.currentThread().getName()
                    + "] Step 2: Credit score = " + creditScore);
                boolean eligible = creditScore >= 700;
                System.out.println("  [" + Thread.currentThread().getName()
                    + "] Step 2: Eligible = " + eligible);
                return eligible;
            })
            // Step 3: Calculate loan amount if eligible
            .thenApply(eligible -> {
                if (!eligible) return "Loan rejected: low credit score";
                BigDecimal loanAmount = new BigDecimal("2500000.00");
                System.out.println("  [" + Thread.currentThread().getName()
                    + "] Step 3: Loan amount = " + loanAmount + " INR");
                return "Loan approved: " + loanAmount + " INR for Rajesh (SAV2001)";
            })
            // Handle any exception in the chain
            .exceptionally(ex -> {
                System.out.println("  ERROR: " + ex.getMessage());
                return "Loan processing failed";
            });

        // Block and get the result (only at the end)
        String result = pipeline.join();
        System.out.println();
        System.out.println("Result: " + result);
    }

    private static void sleep(long ms) {
        try { Thread.sleep(ms); } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
TEXT
Expected output:
=== CompletableFuture Chain ===
Processing loan application for Rajesh (SAV2001)

  [ForkJoinPool.commonPool-worker-1] Step 1: Fetching credit score for SAV2001
  [ForkJoinPool.commonPool-worker-1] Step 2: Credit score = 750
  [ForkJoinPool.commonPool-worker-1] Step 2: Eligible = true
  [ForkJoinPool.commonPool-worker-1] Step 3: Loan amount = 2500000.00 INR

Result: Loan approved: 2500000.00 INR for Rajesh (SAV2001)

How do you build async workflows with CompletableFuture?

The power of CompletableFuture is running multiple tasks in parallel and combining their results. In a banking application, you can run a fraud check and a balance check at the same time, then combine the results to decide whether to approve a payment.

  • thenCombine: runs two futures in parallel and combines their results.
  • allOf: waits for all futures to complete.
  • anyOf: returns the result of the first future that completes.
JAVA
package com.bank.executor;

import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;

public class AsyncWorkflowDemo {
    public static void main(String[] args) {
        System.out.println("=== Parallel Payment Processing for Priya (SAV2002) ===");
        System.out.println("Running fraud check and balance check in parallel...");
        System.out.println();

        long start = System.currentTimeMillis();

        // Fraud check: takes 300ms
        CompletableFuture<Boolean> fraudCheck = CompletableFuture.supplyAsync(() -> {
            System.out.println("  [" + Thread.currentThread().getName()
                + "] Fraud check started for SAV2002");
            sleep(300);
            System.out.println("  [" + Thread.currentThread().getName()
                + "] Fraud check passed");
            return true; // not fraudulent
        });

        // Balance check: takes 200ms
        CompletableFuture<BigDecimal> balanceCheck = CompletableFuture.supplyAsync(() -> {
            System.out.println("  [" + Thread.currentThread().getName()
                + "] Balance check started for SAV2002");
            sleep(200);
            BigDecimal balance = new BigDecimal("350000.00");
            System.out.println("  [" + Thread.currentThread().getName()
                + "] Balance: " + balance + " INR");
            return balance;
        });

        // Combine both results when ready
        BigDecimal paymentAmount = new BigDecimal("75000.00");
        CompletableFuture<String> decision = fraudCheck.thenCombine(balanceCheck,
            (isSafe, balance) -> {
                System.out.println("  [" + Thread.currentThread().getName()
                    + "] Combining results: safe=" + isSafe + ", balance=" + balance);
                if (!isSafe) return "REJECTED: fraud detected";
                if (balance.compareTo(paymentAmount) < 0) return "REJECTED: insufficient funds";
                return "APPROVED: payment of " + paymentAmount + " INR";
            });

        String result = decision.join();
        long elapsed = System.currentTimeMillis() - start;

        System.out.println();
        System.out.println("Decision: " + result);
        System.out.println("Total time: " + elapsed + " ms");
        System.out.println("  (Parallel: ~300ms, not 200+300=500ms sequential)");
    }

    // ====== allOf: wait for multiple independent tasks ======
    public static void allOfExample() {
        System.out.println();
        System.out.println("=== allOf: Batch Account Verification ===");

        CompletableFuture<Void> verify1 = CompletableFuture.runAsync(
            () -> System.out.println("  Verified SAV2001"));
        CompletableFuture<Void> verify2 = CompletableFuture.runAsync(
            () -> System.out.println("  Verified SAV2002"));
        CompletableFuture<Void> verify3 = CompletableFuture.runAsync(
            () -> System.out.println("  Verified CUR3001"));

        CompletableFuture.allOf(verify1, verify2, verify3).join();
        System.out.println("  All accounts verified.");
    }

    private static void sleep(long ms) {
        try { Thread.sleep(ms); } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
TEXT
Expected output:
=== Parallel Payment Processing for Priya (SAV2002) ===
Running fraud check and balance check in parallel...

  [ForkJoinPool.commonPool-worker-1] Fraud check started for SAV2002
  [ForkJoinPool.commonPool-worker-2] Balance check started for SAV2002
  [ForkJoinPool.commonPool-worker-2] Balance: 350000.00 INR
  [ForkJoinPool.commonPool-worker-1] Fraud check passed
  [ForkJoinPool.commonPool-worker-1] Combining results: safe=true, balance=350000.00

Decision: APPROVED: payment of 75000.00 INR
Total time: 315 ms
  (Parallel: ~300ms, not 200+300=500ms sequential)

How is CompletableFuture different from Future?

FeatureFutureCompletableFuture
Blockingget() blocks until result is readyjoin() blocks, but you can chain callbacks to avoid blocking
ChainingNot possible, must call get() then processthenApply, thenCompose, thenCombine chain steps
CombiningCannot combine two FuturesthenCombine, allOf, anyOf combine futures
Exception handlingget() throws ExecutionExceptionexceptionally(), handle() for inline recovery
Manual completionCannot complete from outsidecomplete(), completeExceptionally()
CreationOnly from ExecutorService.submit()supplyAsync, runAsync, or new CompletableFuture()
Cancellationcancel() may not interrupt running taskcancel() plus exceptionally for cleanup
JAVA
package com.bank.executor;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.math.BigDecimal;

public class FutureVsCompletableFutureDemo {
    public static void main(String[] args) throws Exception {
        // Old way: Future blocks
        System.out.println("=== Future (blocking) ===");
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Future<BigDecimal> future = executor.submit(() -> {
            Thread.sleep(100);
            return new BigDecimal("250000.00");
        });
        // BLOCKS here until result is ready
        BigDecimal balance = future.get();
        System.out.println("  Balance: " + balance + " (calling thread was blocked)");
        executor.shutdown();
        executor.awaitTermination(5, TimeUnit.SECONDS);

        System.out.println();

        // New way: CompletableFuture chains
        System.out.println("=== CompletableFuture (non-blocking chain) ===");
        CompletableFuture.supplyAsync(() -> {
                sleep(100);
                return new BigDecimal("250000.00");
            })
            .thenApply(bal -> {
                System.out.println("  Balance: " + bal);
                return bal.compareTo(new BigDecimal("100000")) >= 0;
            })
            .thenAccept(eligible ->
                System.out.println("  Loan eligible: " + eligible + " (no blocking)"))
            .join();
    }

    private static void sleep(long ms) {
        try { Thread.sleep(ms); } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
TEXT
Expected output:
=== Future (blocking) ===
  Balance: 250000.00 (calling thread was blocked)

=== CompletableFuture (non-blocking chain) ===
  Balance: 250000.00
  Loan eligible: true (no blocking)

What about virtual threads and Executors (Java 21+)?

Virtual threads change the thread pool sizing problem. With platform threads, you must carefully choose the pool size: too few threads waste CPU waiting for IO, too many threads waste memory. With virtual threads, you create one per task and let the JVM scheduler handle the rest.

For IO-bound work (database queries, HTTP calls, file reads), replace Executors.newFixedThreadPool(n) with Executors.newVirtualThreadPerTaskExecutor(). For CPU-bound work, continue using newFixedThreadPool(Runtime.getRuntime().availableProcessors()).

JAVA
package com.bank.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.CountDownLatch;
import java.time.Duration;
import java.time.Instant;

public class VirtualThreadExecutorDemo {
    public static void main(String[] args) throws InterruptedException {
        int taskCount = 5000;

        // Platform thread pool: fixed at 50 threads
        System.out.println("=== Fixed Thread Pool (50 platform threads) ===");
        Instant start = Instant.now();
        try (ExecutorService pool = Executors.newFixedThreadPool(50)) {
            CountDownLatch latch = new CountDownLatch(taskCount);
            for (int i = 0; i < taskCount; i++) {
                pool.submit(() -> {
                    try {
                        Thread.sleep(50); // Simulate IO
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    } finally {
                        latch.countDown();
                    }
                });
            }
            latch.await();
        }
        long platformMs = Duration.between(start, Instant.now()).toMillis();
        System.out.println("  " + taskCount + " tasks: " + platformMs + " ms");

        // Virtual thread executor: one virtual thread per task
        System.out.println();
        System.out.println("=== Virtual Thread Executor ===");
        start = Instant.now();
        try (ExecutorService vPool = Executors.newVirtualThreadPerTaskExecutor()) {
            CountDownLatch latch = new CountDownLatch(taskCount);
            for (int i = 0; i < taskCount; i++) {
                vPool.submit(() -> {
                    try {
                        Thread.sleep(50); // Simulate IO
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    } finally {
                        latch.countDown();
                    }
                });
            }
            latch.await();
        }
        long virtualMs = Duration.between(start, Instant.now()).toMillis();
        System.out.println("  " + taskCount + " tasks: " + virtualMs + " ms");

        System.out.println();
        System.out.println("Virtual threads: " + (platformMs / Math.max(virtualMs, 1))
            + "x faster for IO-bound tasks.");
        System.out.println("Reason: all " + taskCount + " virtual threads sleep concurrently,");
        System.out.println("  while only 50 platform threads can sleep at once.");
    }
}
TEXT
Expected output:
=== Fixed Thread Pool (50 platform threads) ===
  5000 tasks: 5120 ms

=== Virtual Thread Executor ===
  5000 tasks: 87 ms

Virtual threads: 58x faster for IO-bound tasks.
Reason: all 5000 virtual threads sleep concurrently,
  while only 50 platform threads can sleep at once.

What are common mistakes with Executors and CompletableFuture?

Wrong: not shutting down the ExecutorService (thread leak)

JAVA
package com.bank.executor;

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

public class WrongNoShutdownDemo {
    public static void main(String[] args) {
        System.out.println("WRONG: ExecutorService never shut down");
        System.out.println();

        ExecutorService executor = Executors.newFixedThreadPool(4);
        executor.submit(() -> System.out.println("  Processing payment for SAV2001"));

        // WRONG: no shutdown() call
        // The JVM will NOT exit because the pool threads are non-daemon.
        // These threads stay alive forever, waiting for new tasks.
        System.out.println("  main() finished, but JVM will NOT exit.");
        System.out.println("  Thread pool is still alive, holding 4 non-daemon threads.");
        System.out.println("  Fix: call executor.shutdown() or use try-with-resources.");

        // Force shutdown for this demo so the JVM exits
        executor.shutdownNow();
    }
}
TEXT
Expected output:
WRONG: ExecutorService never shut down

  Processing payment for SAV2001
  main() finished, but JVM will NOT exit.
  Thread pool is still alive, holding 4 non-daemon threads.
  Fix: call executor.shutdown() or use try-with-resources.

Correct: use try-with-resources (Java 19+)

JAVA
package com.bank.executor;

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

public class CorrectTryWithResourcesDemo {
    public static void main(String[] args) {
        System.out.println("CORRECT: try-with-resources auto-shuts down the executor");
        System.out.println();

        // ExecutorService implements AutoCloseable since Java 19
        // close() calls shutdown() and awaitTermination()
        try (ExecutorService executor = Executors.newFixedThreadPool(4)) {
            executor.submit(() -> System.out.println("  Processing payment for SAV2001"));
            executor.submit(() -> System.out.println("  Processing payment for SAV2002"));
        } // auto-closed here: shutdown + await

        System.out.println("  Executor automatically shut down. JVM will exit cleanly.");
    }
}
TEXT
Expected output:
CORRECT: try-with-resources auto-shuts down the executor

  Processing payment for SAV2001
  Processing payment for SAV2002
  Executor automatically shut down. JVM will exit cleanly.

Wrong: ignoring exceptions in CompletableFuture

JAVA
package com.bank.executor;

import java.util.concurrent.CompletableFuture;

public class WrongIgnoreExceptionDemo {
    public static void main(String[] args) {
        System.out.println("WRONG: exception silently swallowed");
        System.out.println();

        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            throw new RuntimeException("Database connection timeout for SAV2001");
        });

        // WRONG: no exceptionally() or handle(), and no join()
        // The exception is silently lost.
        System.out.println("  Future created but exception is never handled.");
        System.out.println("  The error is invisible. No log, no alert, nothing.");

        // Demonstrate the exception is there
        try {
            future.join();
        } catch (Exception e) {
            System.out.println("  When we finally join: " + e.getCause().getMessage());
        }
    }
}
TEXT
Expected output:
WRONG: exception silently swallowed

  Future created but exception is never handled.
  The error is invisible. No log, no alert, nothing.
  When we finally join: Database connection timeout for SAV2001

Correct: always handle exceptions

JAVA
package com.bank.executor;

import java.util.concurrent.CompletableFuture;

public class CorrectExceptionHandlingDemo {
    public static void main(String[] args) {
        System.out.println("CORRECT: handle exceptions with exceptionally()");
        System.out.println();

        String result = CompletableFuture.supplyAsync(() -> {
                throw new RuntimeException("Database connection timeout for SAV2001");
            })
            .exceptionally(ex -> {
                System.out.println("  [error handler] " + ex.getCause().getMessage());
                return -1; // fallback value
            })
            .thenApply(score -> {
                if (score < 0) return "Loan check failed, using cached data";
                return "Credit score: " + score;
            })
            .join();

        System.out.println("  Result: " + result);
    }
}
TEXT
Expected output:
CORRECT: handle exceptions with exceptionally()

  [error handler] Database connection timeout for SAV2001
  Result: Loan check failed, using cached data

What is a complete banking project with CompletableFuture?

This project builds a payment processing pipeline for National Bank of India. When Priya initiates a payment, three checks run in parallel: fraud detection, balance verification, and daily limit check. When all three pass, the payment is approved and the balance is updated.

Project structure

DIAGRAM
payment-pipeline/
├── pom.xml
└── src/
    └── main/
        └── java/
            └── com/
                └── bank/
                    ├── service/
                    │   ├── FraudService.java            (async fraud check)
                    │   ├── BalanceService.java          (async balance check)
                    │   └── LimitService.java            (async daily limit check)
                    ├── model/
                    │   └── PaymentRequest.java           (payment model)
                    └── PaymentPipelineApp.java           (main class)

pom.xml

XML
<?xml version="1.0" encoding="UTF-8"?syn-kw">>
syn-kw"><project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd"syn-kw">>
    syn-kw"><modelVersionsyn-kw">>4.0.0syn-kw"></modelVersionsyn-kw">>

    syn-kw"><groupIdsyn-kw">>com.banksyn-kw"></groupIdsyn-kw">>
    syn-kw"><artifactIdsyn-kw">>payment-pipelinesyn-kw"></artifactIdsyn-kw">>
    syn-kw"><versionsyn-kw">>1.0.0syn-kw"></versionsyn-kw">>
    syn-kw"><packagingsyn-kw">>jarsyn-kw"></packagingsyn-kw">>

    syn-kw"><propertiessyn-kw">>
        syn-kw"><java.versionsyn-kw">>25syn-kw"></java.versionsyn-kw">>
        syn-kw"><maven.compiler.sourcesyn-kw">>25syn-kw"></maven.compiler.sourcesyn-kw">>
        syn-kw"><maven.compiler.targetsyn-kw">>25syn-kw"></maven.compiler.targetsyn-kw">>
    syn-kw"></propertiessyn-kw">>
syn-kw"></projectsyn-kw">>

PaymentRequest.java

JAVA
package com.bank.model;

import java.math.BigDecimal;

public record PaymentRequest(
    String fromAccount,
    String toAccount,
    BigDecimal amount,
    String initiatedBy
) {}

FraudService.java

JAVA
package com.bank.service;

import com.bank.model.PaymentRequest;
import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;

public class FraudService {
    private static final BigDecimal FRAUD_THRESHOLD = new BigDecimal("1000000.00");

    public CompletableFuture<Boolean> checkFraud(PaymentRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            System.out.println("  [" + Thread.currentThread().getName()
                + "] Fraud check: " + request.fromAccount()
                + ", amount=" + request.amount());
            sleep(300); // Simulate ML model call
            boolean safe = request.amount().compareTo(FRAUD_THRESHOLD) < 0;
            System.out.println("  [" + Thread.currentThread().getName()
                + "] Fraud result: " + (safe ? "SAFE" : "SUSPICIOUS"));
            return safe;
        });
    }

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

BalanceService.java

JAVA
package com.bank.service;

import com.bank.model.PaymentRequest;
import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

public class BalanceService {
    private final ConcurrentHashMap<String, BigDecimal> balances = new ConcurrentHashMap<>(Map.of(
        "SAV2001", new BigDecimal("500000.00"),
        "SAV2002", new BigDecimal("250000.00"),
        "CUR3001", new BigDecimal("1500000.00")
    ));

    public CompletableFuture<Boolean> hasSufficientBalance(PaymentRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            System.out.println("  [" + Thread.currentThread().getName()
                + "] Balance check: " + request.fromAccount());
            sleep(200); // Simulate database call
            BigDecimal balance = balances.getOrDefault(request.fromAccount(), BigDecimal.ZERO);
            boolean sufficient = balance.compareTo(request.amount()) >= 0;
            System.out.println("  [" + Thread.currentThread().getName()
                + "] Balance: " + balance + ", sufficient=" + sufficient);
            return sufficient;
        });
    }

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

LimitService.java

JAVA
package com.bank.service;

import com.bank.model.PaymentRequest;
import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;

public class LimitService {
    private static final BigDecimal DAILY_LIMIT = new BigDecimal("500000.00");

    public CompletableFuture<Boolean> withinDailyLimit(PaymentRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            System.out.println("  [" + Thread.currentThread().getName()
                + "] Limit check: " + request.fromAccount()
                + ", amount=" + request.amount());
            sleep(150); // Simulate limit service call
            boolean withinLimit = request.amount().compareTo(DAILY_LIMIT) <= 0;
            System.out.println("  [" + Thread.currentThread().getName()
                + "] Within daily limit (" + DAILY_LIMIT + "): " + withinLimit);
            return withinLimit;
        });
    }

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

PaymentPipelineApp.java

JAVA
package com.bank;

import com.bank.model.PaymentRequest;
import com.bank.service.FraudService;
import com.bank.service.BalanceService;
import com.bank.service.LimitService;

import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;

public class PaymentPipelineApp {
    public static void main(String[] args) {
        System.out.println("National Bank of India - Payment Pipeline");
        System.out.println("==========================================");
        System.out.println();

        FraudService fraudService = new FraudService();
        BalanceService balanceService = new BalanceService();
        LimitService limitService = new LimitService();

        PaymentRequest request = new PaymentRequest(
            "SAV2001", "SAV2002",
            new BigDecimal("75000.00"),
            "Priya"
        );

        System.out.println("Payment: " + request.initiatedBy()
            + " sends " + request.amount() + " INR"
            + " from " + request.fromAccount()
            + " to " + request.toAccount());
        System.out.println();

        long start = System.currentTimeMillis();

        // Run all three checks in parallel
        CompletableFuture<Boolean> fraud = fraudService.checkFraud(request);
        CompletableFuture<Boolean> balance = balanceService.hasSufficientBalance(request);
        CompletableFuture<Boolean> limit = limitService.withinDailyLimit(request);

        // Wait for all three and combine
        String decision = CompletableFuture.allOf(fraud, balance, limit)
            .thenApply(v -> {
                boolean fraudOk = fraud.join();
                boolean balanceOk = balance.join();
                boolean limitOk = limit.join();

                System.out.println();
                System.out.println("  Fraud safe:         " + fraudOk);
                System.out.println("  Sufficient balance: " + balanceOk);
                System.out.println("  Within daily limit: " + limitOk);

                if (fraudOk && balanceOk && limitOk) {
                    return "APPROVED";
                }
                return "REJECTED";
            })
            .exceptionally(ex -> {
                System.out.println("  Pipeline error: " + ex.getMessage());
                return "ERROR";
            })
            .join();

        long elapsed = System.currentTimeMillis() - start;

        System.out.println();
        System.out.println("Decision: " + decision);
        System.out.println("Total time: " + elapsed + " ms");
        System.out.println("  (Parallel execution: limited by slowest check, not sum of all)");
    }
}
TEXT
Expected output:
National Bank of India - Payment Pipeline
==========================================

Payment: Priya sends 75000.00 INR from SAV2001 to SAV2002

  [ForkJoinPool.commonPool-worker-1] Fraud check: SAV2001, amount=75000.00
  [ForkJoinPool.commonPool-worker-2] Balance check: SAV2001
  [ForkJoinPool.commonPool-worker-3] Limit check: SAV2001, amount=75000.00
  [ForkJoinPool.commonPool-worker-3] Within daily limit (500000.00): true
  [ForkJoinPool.commonPool-worker-2] Balance: 500000.00, sufficient=true
  [ForkJoinPool.commonPool-worker-1] Fraud result: SAFE

  Fraud safe:         true
  Sufficient balance: true
  Within daily limit: true

Decision: APPROVED
Total time: 320 ms
  (Parallel execution: limited by slowest check, not sum of all)

The total time is approximately 300ms (the slowest check: fraud), not 650ms (300+200+150). All three checks ran concurrently.

What should a senior or principal engineer know?

At the senior level, you should understand thread pool sizing, queue policies, and CompletableFuture execution contexts.

  • ThreadPoolExecutor parameters: Use new ThreadPoolExecutor(core, max, keepAlive, unit, queue) instead of Executors factory methods when you need control over queue type (bounded vs unbounded), rejection policy, and thread factory.
  • Rejection policies: When the pool and queue are full, ThreadPoolExecutor applies a rejection policy: AbortPolicy (throws exception, default), CallerRunsPolicy (caller thread runs the task), DiscardPolicy (silently drops), DiscardOldestPolicy (drops oldest queued task).
  • Bounded queues: Executors.newFixedThreadPool() uses an unbounded queue, which can cause OutOfMemoryError if tasks accumulate faster than processing. In production, use a bounded ArrayBlockingQueue with CallerRunsPolicy for back-pressure.
  • CompletableFuture default executor: By default, supplyAsync and runAsync use the ForkJoinPool.commonPool(), which has availableProcessors() - 1 threads. For IO-bound work, pass a custom executor: supplyAsync(task, ioExecutor).
  • Virtual threads with CompletableFuture: Pass Executors.newVirtualThreadPerTaskExecutor() as the executor to supplyAsync for IO-bound CompletableFuture pipelines.
  • Structured concurrency (Java 25 preview): StructuredTaskScope replaces CompletableFuture for structured fork-join patterns. ShutdownOnFailure cancels sibling tasks when one fails. ShutdownOnSuccess cancels siblings when one succeeds.
JAVA
package com.bank.executor;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.RejectedExecutionHandler;

public class SeniorExecutorKnowledgeDemo {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("=== Production-Grade ThreadPoolExecutor ===");

        // Custom thread pool with bounded queue and caller-runs policy
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
            4,                                      // core pool size
            8,                                      // max pool size
            60, TimeUnit.SECONDS,                   // idle thread timeout
            new ArrayBlockingQueue<>(100),           // bounded queue (100 tasks max)
            new ThreadPoolExecutor.CallerRunsPolicy() // back-pressure: caller runs the task
        );

        System.out.println("Core pool:     " + executor.getCorePoolSize());
        System.out.println("Max pool:      " + executor.getMaximumPoolSize());
        System.out.println("Queue capacity: 100 (bounded)");
        System.out.println("Rejection:     CallerRunsPolicy (back-pressure)");
        System.out.println();

        // Submit tasks
        for (int i = 0; i < 10; i++) {
            final int task = i;
            executor.submit(() -> {
                System.out.println("  Task " + task + " on " + Thread.currentThread().getName());
                try { Thread.sleep(50); } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
        }

        executor.shutdown();
        executor.awaitTermination(10, TimeUnit.SECONDS);

        System.out.println();
        System.out.println("Completed tasks: " + executor.getCompletedTaskCount());
        System.out.println();

        System.out.println("=== Senior/Principal Checklist ===");
        System.out.println("1. Never use Executors.newFixedThreadPool in production without");
        System.out.println("   considering the unbounded queue (OutOfMemoryError risk).");
        System.out.println("2. Use bounded ArrayBlockingQueue + CallerRunsPolicy for back-pressure.");
        System.out.println("3. CompletableFuture uses ForkJoinPool.commonPool by default.");
        System.out.println("   Pass a custom executor for IO-bound tasks.");
        System.out.println("4. Virtual thread executor for IO-bound CompletableFuture pipelines.");
        System.out.println("5. Monitor: pool size, active count, queue size, completed tasks.");
        System.out.println("6. Structured concurrency (Java 25 preview) for fork-join patterns.");
    }
}
TEXT
Expected output:
=== Production-Grade ThreadPoolExecutor ===
Core pool:     4
Max pool:      8
Queue capacity: 100 (bounded)
Rejection:     CallerRunsPolicy (back-pressure)

  Task 0 on pool-1-thread-1
  Task 1 on pool-1-thread-2
  Task 2 on pool-1-thread-3
  Task 3 on pool-1-thread-4
  Task 4 on pool-1-thread-1
  Task 5 on pool-1-thread-2
  Task 6 on pool-1-thread-3
  Task 7 on pool-1-thread-4
  Task 8 on pool-1-thread-1
  Task 9 on pool-1-thread-2

Completed tasks: 10

=== Senior/Principal Checklist ===
1. Never use Executors.newFixedThreadPool in production without
   considering the unbounded queue (OutOfMemoryError risk).
2. Use bounded ArrayBlockingQueue + CallerRunsPolicy for back-pressure.
3. CompletableFuture uses ForkJoinPool.commonPool by default.
   Pass a custom executor for IO-bound tasks.
4. Virtual thread executor for IO-bound CompletableFuture pipelines.
5. Monitor: pool size, active count, queue size, completed tasks.
6. Structured concurrency (Java 25 preview) for fork-join patterns.

What are common interview follow-up questions?

Visual Overview

 Executor Framework Architecture
 =================================

 ┌────────────────────────────────────────────────┐
 │              ExecutorService                    │
 │                                                │
 │  ┌─────────────────────────────────────────┐   │
 │  │           Task Queue                     │   │
 │  │  [Task1] [Task2] [Task3] [Task4] [Task5]│   │
 │  └──────────────────┬──────────────────────┘   │
 │                     │                          │
 │         ┌───────────┼───────────┐              │
 │         ▼           ▼           ▼              │
 │  ┌──────────┐ ┌──────────┐ ┌──────────┐       │
 │  │ Worker 1 │ │ Worker 2 │ │ Worker 3 │       │
 │  │ (thread) │ │ (thread) │ │ (thread) │       │
 │  └──────────┘ └──────────┘ └──────────┘       │
 │                                                │
 │  Workers pick tasks from the queue.            │
 │  When a worker finishes, it picks the next.    │
 └────────────────────────────────────────────────┘


 CompletableFuture Pipeline
 ============================

 ┌────────────────┐
 │ supplyAsync()  │ Step 1: Fetch credit score
 │ (async, pool)  │ Returns: 750
 └───────┬────────┘
         │ thenApply()
         ▼
 ┌────────────────┐
 │ Transform      │ Step 2: Check eligibility
 │ 750 -> true    │ Returns: true
 └───────┬────────┘
         │ thenApply()
         ▼
 ┌────────────────┐
 │ Transform      │ Step 3: Calculate loan
 │ true -> 25L    │ Returns: "Approved: 25L"
 └───────┬────────┘
         │ exceptionally()
         ▼
 ┌────────────────┐
 │ Error handler  │ If any step fails
 │ fallback value │ Returns: "Failed"
 └───────┬────────┘
         │ join()
         ▼
      Result


 Parallel Execution with thenCombine
 =====================================

 ┌──────────────┐    ┌──────────────┐
 │ Fraud Check  │    │Balance Check │
 │ (300 ms)     │    │ (200 ms)     │
 └──────┬───────┘    └──────┬───────┘
        │                   │
        └───────┬───────────┘
                │ thenCombine()
                ▼
        ┌───────────────┐
        │ Combine:      │
        │ safe AND      │
        │ sufficient    │
        │ -> APPROVED   │
        └───────────────┘

 Total time: ~300 ms (not 500 ms)


 Virtual Threads vs Platform Thread Pool
 =========================================

 Platform (50 threads, 5000 tasks):
 ┌──────────────────────────────────────┐
 │ [T1][T2][T3]...[T50]  <- 50 threads │
 │ [Queue: 4950 waiting tasks]         │
 │ Each task waits for a free thread.  │
 └──────────────────────────────────────┘
 Time: ~5000 ms

 Virtual (5000 threads, 5000 tasks):
 ┌──────────────────────────────────────┐
 │ [V1][V2][V3]...[V5000]              │
 │ All 5000 tasks run concurrently.    │
 │ JVM multiplexes onto ~8 carriers.   │
 └──────────────────────────────────────┘
 Time: ~80 ms

Key Takeaway

The Executor framework decouples task submission from thread management. Instead of creating a new thread for every task, you submit tasks to an ExecutorService that maintains a pool of reusable worker threads. Java provides fixed thread pools for steady workloads, cached thread pools for bursty short tasks, single-thread executors for sequential processing, and scheduled executors for periodic tasks. Since Java 21, the virtual thread executor creates one lightweight thread per task, ideal for IO-bound workloads like database queries and HTTP calls.

CompletableFuture builds on this foundation by letting you chain async operations without blocking. You start with supplyAsync to run a task in the background, then chain thenApply to transform the result, thenCompose to chain another async step, and thenCombine to merge two parallel results. The exceptionally method catches any error in the chain and provides a fallback. This lets you build pipelines like "run fraud check and balance check in parallel, then combine results to approve or reject a payment."

In our National Bank of India payment pipeline project, Priya initiated a payment of 75,000 INR from SAV2001 to SAV2002. Three checks ran in parallel: fraud detection (300ms), balance verification (200ms), and daily limit check (150ms). Because they ran concurrently, the total time was about 300ms (the slowest check), not 650ms (the sum). CompletableFuture.allOf waited for all three, and the combining step checked that all passed before approving the payment.

At the senior level, avoid the unbounded queue in Executors.newFixedThreadPool, which can cause OutOfMemoryError under sustained load. Use ThreadPoolExecutor with a bounded ArrayBlockingQueue and CallerRunsPolicy for back-pressure. Know that CompletableFuture uses ForkJoinPool.commonPool by default, which has only availableProcessors() - 1 threads, not enough for IO-bound tasks. Pass a custom executor or a virtual thread executor for IO-heavy pipelines. At the principal level, adopt structured concurrency (Java 25 preview) with StructuredTaskScope for fork-join patterns that automatically cancel sibling tasks on failure, and design thread pool configurations based on measured workload characteristics: CPU-bound tasks need cores threads, IO-bound tasks need more or virtual threads.

Remember one sentence: the Executor framework reuses a pool of threads to run submitted tasks, and CompletableFuture chains async steps with thenApply, thenCompose, and thenCombine to build non-blocking pipelines.

---