Definition
- 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 submitCallabletasks that returnFutureresults. - 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
Futurethat 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 onlyexecute(Runnable).ExecutorService: addssubmit()(returns a Future),shutdown(), andawaitTermination().ScheduledExecutorService: addsschedule(),scheduleAtFixedRate(), andscheduleWithFixedDelay().
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.");
}
}
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 Pool | Threads | Queue | Best For |
|---|---|---|---|
| newFixedThreadPool(n) | Fixed at n | Unbounded LinkedBlockingQueue | Known, steady workload (batch processing) |
| newCachedThreadPool() | 0 to Integer.MAX_VALUE, idle threads expire in 60s | SynchronousQueue (no capacity) | Short-lived burst tasks |
| newSingleThreadExecutor() | Exactly 1 | Unbounded LinkedBlockingQueue | Sequential task execution (event log) |
| newScheduledThreadPool(n) | Fixed core of n | DelayedWorkQueue | Periodic tasks (report generation, cleanup) |
| newVirtualThreadPerTaskExecutor() | One virtual thread per task | No queue (immediate dispatch) | IO-bound tasks (database, HTTP calls) |
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);
}
}
}
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?
| Feature | newFixedThreadPool(n) | newCachedThreadPool() |
|---|---|---|
| Thread count | Always n threads | 0 to Integer.MAX_VALUE |
| Idle threads | Stay alive forever | Expire after 60 seconds |
| Queue | Unbounded LinkedBlockingQueue | SynchronousQueue (zero capacity) |
| Overflow behavior | Tasks queue up (no new threads created) | New threads created immediately |
| Risk | Queue grows unbounded if tasks arrive faster than processing (OutOfMemoryError) | Thread count grows unbounded under sustained load (OutOfMemoryError) |
| Use case | Steady, predictable workload | Short burst of many small tasks |
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.)");
}
}
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 (likemapon a stream).thenCompose(Function): chains another CompletableFuture (likeflatMap).thenCombine(CompletableFuture, BiFunction): combines two results.exceptionally(Function): handles exceptions.thenAccept(Consumer): consumes the result without returning a new value.
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();
}
}
}
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.
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();
}
}
}
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?
| Feature | Future | CompletableFuture |
|---|---|---|
| Blocking | get() blocks until result is ready | join() blocks, but you can chain callbacks to avoid blocking |
| Chaining | Not possible, must call get() then process | thenApply, thenCompose, thenCombine chain steps |
| Combining | Cannot combine two Futures | thenCombine, allOf, anyOf combine futures |
| Exception handling | get() throws ExecutionException | exceptionally(), handle() for inline recovery |
| Manual completion | Cannot complete from outside | complete(), completeExceptionally() |
| Creation | Only from ExecutorService.submit() | supplyAsync, runAsync, or new CompletableFuture() |
| Cancellation | cancel() may not interrupt running task | cancel() plus exceptionally for cleanup |
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();
}
}
}
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()).
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.");
}
}
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)
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();
}
}
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+)
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.");
}
}
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
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());
}
}
}
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
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);
}
}
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
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 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
package com.bank.model;
import java.math.BigDecimal;
public record PaymentRequest(
String fromAccount,
String toAccount,
BigDecimal amount,
String initiatedBy
) {}
FraudService.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
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
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
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)");
}
}
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 ofExecutorsfactory 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,
ThreadPoolExecutorapplies 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 causeOutOfMemoryErrorif tasks accumulate faster than processing. In production, use a boundedArrayBlockingQueuewithCallerRunsPolicyfor back-pressure. - CompletableFuture default executor: By default,
supplyAsyncandrunAsyncuse theForkJoinPool.commonPool(), which hasavailableProcessors() - 1threads. For IO-bound work, pass a custom executor:supplyAsync(task, ioExecutor). - Virtual threads with CompletableFuture: Pass
Executors.newVirtualThreadPerTaskExecutor()as the executor tosupplyAsyncfor IO-bound CompletableFuture pipelines. - Structured concurrency (Java 25 preview):
StructuredTaskScopereplaces CompletableFuture for structured fork-join patterns.ShutdownOnFailurecancels sibling tasks when one fails.ShutdownOnSuccesscancels siblings when one succeeds.
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.");
}
}
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?
- What is the difference between submit() and execute()? (execute() takes a Runnable and returns void. submit() takes a Runnable or Callable and returns a Future. submit() also swallows exceptions unless you call Future.get().)
- What happens if a task throws an exception in a thread pool? (The exception is caught by the thread pool and stored in the Future. If you never call get() or join(), you never see the exception. The thread is not killed, it returns to the pool.)
- How do you size a thread pool? (For CPU-bound: threads = number of CPU cores. For IO-bound: threads = CPU cores * (1 + wait_time / compute_time). Or use virtual threads for IO-bound work.)
- What is the difference between shutdown() and shutdownNow()? (shutdown() stops accepting new tasks but finishes running and queued tasks. shutdownNow() interrupts running tasks and returns the list of queued tasks that were not started.)
- Can you use CompletableFuture with virtual threads? (Yes. Pass Executors.newVirtualThreadPerTaskExecutor() as the second argument to supplyAsync or runAsync.)
- What is thenCompose vs thenApply? (thenApply transforms the value: CompletableFuture to CompletableFuture. thenCompose chains another async step: the function returns a CompletableFuture, and the result is flattened. thenCompose is to CompletableFuture what flatMap is to Stream.)
- What is the danger of newCachedThreadPool? (It creates a new thread for every task if no idle thread is available. Under sustained load, it can create thousands of threads and crash with OutOfMemoryError. Use newFixedThreadPool or virtual threads for production workloads.)
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.
---