Introduced in Java 5 inside java.util.concurrent, the Executor Framework abstracts away the low-level complexities of managing raw thread lifecycles by introducing high-performance Thread Pools.
ExecutorService & Callable<V>import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Callable;
public class ExecutorServiceDemo {
public static void main(String[] args) throws Exception {
// Create a fixed thread pool with 3 worker threads
ExecutorService executor = Executors.newFixedThreadPool(3);
// Submit a Callable task that returns a computed result
Callable<Integer> calculation = () -> {
System.out.println("Processing complex calculation on " + Thread.currentThread().getName());
Thread.sleep(1000);
return 42 * 10;
};
// Submit returns a Future holding the asynchronous result
Future<Integer> futureResult = executor.submit(calculation);
System.out.println("Main thread continues doing other work...");
// .get() blocks until the worker thread produces the result:
Integer result = futureResult.get();
System.out.println("Calculation Result: " + result); // 420
// Always shutdown executors cleanly!
executor.shutdown();
}
}
Java 21 introduced the biggest concurrency leap in 25 years: Virtual Threads (Project Loom). Proceed to: Java 21 Virtual Threads →
Accelerate your Java mastery by studying these tightly connected core tutorials:
The future of Java concurrency.
Thread fundamentals.
Thread safety.
Submitting tasks via lambdas.