Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions
What is the Executor framework in Java?
The Executor framework, in java.util.concurrent, separates task submission from the mechanics of how and when each task runs on a thread. Instead of manually creating and managing Thread objects, you submit Runnable or Callable tasks to an Executor or ExecutorService.
The Executors factory class provides ready-made thread pool configurations, such as newFixedThreadPool, newCachedThreadPool, newScheduledThreadPool, and, since Java 21, newVirtualThreadPerTaskExecutor.
ExecutorService pool = Executors.newFixedThreadPool(4); Future<Integer> result = pool.submit(() -> compute()); pool.shutdown();
Under the hood, an ExecutorService reuses a bounded or unbounded set of worker threads and queues incoming tasks, which avoids the cost of spinning up a new OS thread for every task and gives you central control over concurrency limits and shutdown behavior.
More Related questions...