Java › Concurrency & Threads
What is the ExecutorService framework, and why is it generally preferred over manually creating and managing individual Thread objects?
IntermediateAnswer
ExecutorService decouples task submission from the mechanics of thread creation, pooling, and lifecycle management -- a fixed or cached thread pool (created via Executors factory methods, or more explicitly via ThreadPoolExecutor for fine control) reuses a bounded set of worker threads across many submitted tasks, avoiding the overhead of creating a brand-new OS thread per task, providing built-in mechanisms for graceful shutdown, task result retrieval via Future, and protecting against unbounded thread creation that could otherwise exhaust system resources under heavy load.
ExecutorService executor = Executors.newFixedThreadPool(4);
Future<Integer> future = executor.submit(() -> {
// some computation
return 42;
});
try {
Integer result = future.get(5, TimeUnit.SECONDS); // blocks with a timeout
} catch (TimeoutException e) {
future.cancel(true);
}
executor.shutdown(); // initiates graceful shutdown, letting in-flight tasks completeReal-world example A web service handling incoming requests submits each request's processing work to a fixed-size ExecutorService thread pool rather than spawning a new Thread per request, bounding the maximum concurrent OS thread count and preventing resource exhaustion under a traffic spike that an unbounded thread-per-request model would risk.