CompletableFuture in Java 8, asynchronous processing done right
summary
Live coding shows how Java 8’s CompletableFuture replaces blocking threads with declarative pipelines: creating futures via supplyAsync using common ForkJoinPool (or with a passed Executor), chaining transforms with thenApply and thenCompose to avoid future‑of‑future, combining concurrency with thenCombine, applyToEither, allOf/anyOf (note allOf returns Void), and handling errors with handle/exceptionally. Also demonstrates promises via new CompletableFuture plus complete/completeExceptionally, and a timeout pattern built by racing a scheduled exceptional future against the real task.
AI-written summary · about
chapters (17)
- 0:00Introduction to CompletableFuture
- 1:13Problem with blocking futures
- 3:40Introducing CompletableFuture
- 8:53Creating CompletableFuture instances
- 13:03Transforming futures with callbacks
- 19:03Chaining transformations
- 25:23Avoiding callback hell
- 27:48Building async pipelines
- 30:14Combining multiple futures
- 33:21Handling first completed future
- 34:02Waiting for all futures
- 36:08Handling exceptions in pipelines
- 41:22Error recovery with handle
- 48:33Creating futures manually
- 50:38Timeout patterns with futures
- 56:04Refactoring callback APIs
- 1:00:19Summary and Q&A
