Skip to main content
Module 9 · Lesson 1 of 219/22 in the course~17 min
Module lessons (1/2)

ExecutorService and CompletableFuture

Lesson preview5 hands-on exercises readyCourse Java · Module 9 — Concurrency and File I/O (NIO.2)

Separate tasks from threads with pools ExecutorService, asynchronous pipelines CompletableFuture, ConcurrentHashMap, and AtomicInteger.

What you'll learn in this lesson

  • Run concurrent tasks on managed thread pools via ExecutorService without creating disposable threads.
  • Collect future results with Future and always release workers with shutdown().
  • Compose non-blocking asynchronous pipelines with CompletableFuture, thenApply, thenCombine and exceptionally.

Overview

Manually creating new Thread(...).start() for each request consumes native stack memory and makes the number of active threads uncontrollable. In modern Java, you separate tasks from worker threads via ExecutorService, and combine asynchronous streams with CompletableFuture.

A ExecutorService maintains a fixed or elastic pool of ready threads. submit() accepts a Callable<T> or Runnable and immediately returns a Future<T> to query when the result is needed:

Sections covered in this lesson

  1. 11. Thread Pool with ExecutorService and Future
  2. 22. Asynchronous Pipelines with CompletableFuture
  3. 33. Combine Two Streams and Handle Asynchronous Errors
  4. 44. Shared State: AtomicInteger against Race Condition
  5. 5Try it yourself
Pro Lesson · Module 9 — Concurrency and File I/O (NIO.2)

Unlock "ExecutorService and CompletableFuture" and the full Course Java curriculum

Separate tasks from threads with pools `ExecutorService`, asynchronous pipelines `CompletableFuture`, `ConcurrentHashMap`, and `AtomicInteger`.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Java plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.