00:00

Java Executor Framework Interview Questions and Answers

Last updated:

These are the Executor Framework questions that come up most often in Java interviews, from the basics to the changes in recent releases. Each answer is short enough to say out loud and links to the full tutorial if you want more depth.

1. Basic Questions

1. What is the Executor Framework, and why use it instead of creating threads?

It is the java.util.concurrent API, added in Java 5, that separates submitting a task from deciding how it runs. Executors reuse threads, limit how many run at once, queue extra work and return results through a Future. See the Executor Framework tutorial.

2. What is the difference between Executor, ExecutorService and Executors?

Executor is an interface with one method, execute(Runnable). ExecutorService extends it with submit(), bulk execution and lifecycle methods. Executors is a utility class of static factory methods that create executors.

3. What is the difference between Runnable and Callable?

Runnable.run() returns nothing and cannot throw checked exceptions. Callable.call() returns a value and can throw any exception. See Runnable vs Callable.

4. What is the difference between execute() and submit()?

execute() takes a Runnable and returns nothing; an exception reaches the worker thread's uncaught exception handler. submit() accepts a Runnable or Callable and returns a Future that stores the result or the exception.

5. What does Future.get() do?

It waits until the task finishes and returns its result. If the task threw, it throws ExecutionException with the original as its cause; if the task was cancelled, it throws CancellationException. A timed overload throws TimeoutException.

2. Intermediate Questions

6. What is the difference between shutdown(), shutdownNow() and close()?

shutdown() rejects new tasks but lets submitted ones finish, without waiting. shutdownNow() also interrupts running tasks and returns the queued ones. close(), added in Java 19, shuts down and waits.

7. What is the difference between invokeAll() and invokeAny()?

invokeAll() waits for every task and returns their futures in task order. invokeAny() returns the result of one task that completed successfully and cancels the rest.

8. How does ThreadPoolExecutor decide whether to queue, grow or reject a task?

Below the core size it starts a new thread. Otherwise it queues the task. If the queue is full, it adds a thread up to the maximum size; if that is impossible, it rejects the task. See ThreadPoolExecutor.

9. What rejection policies does ThreadPoolExecutor provide?

AbortPolicy (the default, throws RejectedExecutionException), CallerRunsPolicy (runs the task on the submitting thread), DiscardPolicy and DiscardOldestPolicy.

10. Why can Executors.newFixedThreadPool() be risky in production?

It uses an unbounded LinkedBlockingQueue, so under sustained overload the queue keeps growing and can exhaust memory. A ThreadPoolExecutor with a bounded queue and an explicit rejection policy avoids this.

11. What is the difference between scheduleAtFixedRate() and scheduleWithFixedDelay()?

Fixed rate plans each start relative to the first one; fixed delay waits a set time after each run ends. Runs of the same periodic task never overlap, and an exception suppresses later runs.

3. Advanced Questions

12. When would you use a CompletionService?

When you want results in the order tasks finish, not the order you submitted them: for example, showing the first quotes that arrive. ExecutorCompletionService wraps any executor and exposes take() and poll().

13. How is CompletableFuture different from Future?

A Future can only be waited on or cancelled. A CompletableFuture can be completed manually and chained without blocking, using thenApply(), thenCompose(), thenCombine() and error handlers such as exceptionally().

14. What is work-stealing in ForkJoinPool?

Each worker has its own task queue, and idle workers take tasks from busy ones. It suits divide-and-conquer work split with RecursiveTask and RecursiveAction.

15. Should you pool virtual threads?

No. Oracle's guidance is to create one virtual thread per task, for example with Executors.newVirtualThreadPerTaskExecutor() (Java 21+), and to limit access to scarce resources with a Semaphore instead of a small pool.

16. What changed in the Executor Framework between Java 19 and Java 25?

Java 19 made ExecutorService AutoCloseable and added Future's state(), resultNow() and exceptionNow(). Java 21 added virtual-thread executors. Java 25 made ForkJoinPool implement ScheduledExecutorService; structured concurrency is still a preview API in Java 25.

4. Predict the Output

A favourite whiteboard question. Work out what this program prints before reading the answer.

PredictTheOutput.java
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class PredictTheOutput {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        try (ExecutorService executor = Executors.newSingleThreadExecutor()) {
            Future<?> a = executor.submit(() -> {
                throw new IllegalStateException("task A broke");
            });
            executor.execute(() -> System.out.println("B runs"));
            Future<Integer> c = executor.submit(() -> 20 + 22);

            System.out.println("C = " + c.get());
            try {
                a.get();
            } catch (ExecutionException e) {
                System.out.println("A failed: " + e.getCause().getMessage());
            }
        }
    }
}
Output
B runs
C = 42
A failed: task A broke

Why

  1. A single-thread executor runs tasks one at a time, in submission order: A, then B, then C.
  2. Task A throws, but because it was passed to submit(), the exception is stored in its Future and nothing is printed yet.
  3. B prints while main is still waiting in c.get(), which returns 42 after C runs.
  4. Only a.get() reveals A's failure, wrapped in ExecutionException.

5. Summary

  • Know the three core types: Executor, ExecutorService and the Executors factory class.
  • Be precise about execute() vs submit() and how exceptions reach the caller.
  • Explain how ThreadPoolExecutor queues, grows and rejects tasks, and why unbounded queues are risky.
  • Mention modern APIs: close(), Future.state(), virtual-thread executors and Java 25 ForkJoinPool scheduling.

Related Tutorials

6. References