Chapter 13. Asynchronous Tasks
Modern workloads require more thought about how to use available system resources efficiently. Asynchronous tasks are an excellent tool for improving the responsiveness of your application by avoiding performance bottlenecks.
Java 8 introduced the new generic type, CompletableFuture<T>
, which improved upon the previously available Future<T>
type to create async tasks by utilizing a declarative and functional approach.
This chapter explains why and how to utilize asynchronous programming and how CompletableFuture<T>
is a more flexible and functional approach to asynchronous tasks than what was included in the JDK before.
Synchronous versus Asynchronous
The concept of synchronous and asynchronous tasks is not restricted to software development.
For example, an in-person meeting or conference call is a synchronous activity, at least if you pay attention. You can’t do anything else except participate and maybe take notes. Every other task is blocked until the meeting/call is over. If the meeting/call had been an e-mail instead—as most of my meetings could and should be—your current task isn’t interrupted by requiring immediate attention before you could resume your previous task. Therefore, an e-mail is non-blocking communication.
The same principles are true for software development. Synchronously executed tasks run in sequence, blocking further work until they’re finished. From a single-threaded point of view, a blocking task means waiting for the ...
Get A Functional Approach to Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.