Category: javascript
ExecutorService
Published on 22 Feb 2026
Explanation
ExecutorService is a framework in Java
used to manage and control thread execution asynchronously.
Code:
ExecutorService executor =
Executors.newFixedThreadPool(2);
executor.submit(() ->
System.out.println("Task executed"));
executor.shutdown();
Explanation
ExecutorService helps in reusing
threads from a pool
instead of creating new threads manually.
Code:
ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(() -> System.out.println(Thread.currentThread().getName())); executor.shutdown();
Explanation
submit() method returns a Future object
which can be used to get the result of a
task.
Code:
ExecutorService executor = Executors.newSingleThreadExecutor(); Future<Integer> result = executor.submit(() -> 10 + 20); System.out.println(result.get()); executor.shutdown();
Explanation
shutdown() initiates an orderly shutdown
where previously submitted tasks are executed.
Code:
executor.shutdown();
Explanation
shutdownNow() attempts to stop all
actively executing tasks immediately.
Code:
executor.shutdownNow();