Java developers generally prefer synchronous code because the request/response approach is simpler and fits the programmer’s mindset. This is fine until the system hits a performance bottleneck. With synchronous programming, each thread can only initiate one request at a time and wait for it to return synchronously. Therefore, in order to improve system performance, we need to introduce more threads to achieve parallel processing. However, when accessing shared resources in multithreading, resource contention and concurrency will inevitably be introduced. In addition, the operating system level is limited to the number of threads, it is impossible to increase the number of threads to provide system performance; Moreover, the use of synchronous blocking programming will also waste resources, for example, when initiating network IO request, the calling thread will be in the state of synchronous blocking waiting for the response result, when the calling thread clearly can do other things, and wait for the network IO response result to process the result.

It can be seen that parallel programming by increasing the number of single-machine system threads is not a “panacea”. By writing asynchronous, non-blocking code, you can improve system performance by switching execution to another active task using the same underlying resources, and then returning to the current thread to continue processing when the asynchronous processing is complete.

Asynchronous programming is a means of allowing programs to run in parallel by having a unit of work run separately from the main application thread, and notifies the main application thread of the result or failure of the unit of work when it has finished. Using asynchronous programming can improve application performance and responsiveness, among other things.

Such as when the calling thread initiated using asynchronous network IO request, the calling thread is not synchronized block waiting for a response as a result, but after the memory to save the request context, immediately return to do other things, such as network IO response results back before using IO thread thread response result notification business has returned, by business threads to deal with the results. As you can see, asynchronous invocation improves thread utilization, allowing the system to have more thread resources to handle more requests. In mobile application, for example, the user operating mobile screen after a request, if be synchronous waiting for backend server returns as a result, the service operation when the background is very time consuming, can cause the user sees mobile screen freeze (a straight in request processing), before the results back, user can’t operate other functions of a mobile device, This is very bad for the user experience. With asynchronous programming, when a request is made, the calling thread returns immediately, and the results are rendered asynchronously by the UI thread, during which time the user can use other functions of the mobile device.

This study note shared today is a top tutorial note for Java asynchronous programming!

Due to the content of the notes too much, the following can only show you part of the content, the full Blu-ray version of free access at the end of the article!

Chapter Contents

Chapter 1 introduces asynchronous programming

This chapter mainly introduces the concept and functions of asynchronous programming, asynchronous programming scenarios in Java, and what technologies should be used in different asynchronous programming scenarios.

Chapter 2 explicitly uses threads and thread pools for asynchronous programming

This chapter focuses on how to explicitly use threads and thread pools for asynchronous programming, including how to explicitly use threads for asynchronous programming and the disadvantages of using threads for asynchronous programming, how to explicitly use thread pools for asynchronous programming, and how to implement thread pools.

Chapter 3 implements asynchronous programming based on the Future in the JDK

This chapter focuses on how to implement asynchronous programming using Future in the JDK, including how to implement asynchronous programming using FutureTask and its internal implementation principles. How to use CompletableFuture to realize asynchronous programming and its internal implementation principle, and how CompletableFuture and JDK Stream are perfectly combined.

Chapter 4: Asynchronous execution in the Spring framework

The Spring Framework uses the TaskExecutor and TaskScheduler interfaces to provide abstractions for asynchronous execution and task scheduling, respectively. In this chapter, we will focus on how the @asyne annotation supported by TaskExecutor implements asynchronous processing.

Chapter 5 realizes asynchronous programming based on reactive programming

This chapter mainly explains how to use reactive programming to realize asynchronous programming, including what is reactive programming, why it is needed, what are the characteristics and values of reactive programming, and how to realize asynchronous programming based on the reactive programming implementation library RxJava and Reactor.

Chapter 6. Asynchronous non-blocking processing of Web Servlets

This chapter focuses on the synchronous processing model and disadvantages before the Servlet3.0 specification, the asynchronous processing capability provided by the Servlet3.0 specification and the non-blocking I0 capability provided by the Servlet3.1 specification, as well as the asynchronous processing capability provided by Spring MVC.

Chapter 7: Asynchronous non-blocking processing for Spring WebFlux

This chapter mainly introduces the new WebFlux technology stack introduced in Spring Framework 5.0, and introduces its value and significance, concurrency model and application scenarios, how to realize asynchronous programming based on WebFlux, and its internal implementation principle.

Chapter 8 high-performance asynchronous programming frameworks and middleware

This chapter mainly introduces some high-performance asynchronous programming frameworks and middleware, including the asynchronous, event-driven network programming framework — Netty; A high-performance RPC framework Apache Dubbo; High-performance interthread messaging library Disruptor; An asynchronous, distributed, message-driven framework — Akka; High-performance distributed messaging framework Apache RocketMQ.

Chapter 9: Asynchronous programming capabilities of Go

This chapter mainly explains the asynchronous programming ability of Go language, including the thread model of Go language, and how to use the primitive Goroutine and channel to realize asynchronous programming, finally based on Goroutine and channel to build a backpressure, asynchronous programming pipeline.

For those who need Java Asynchronous programming notes in Blu-ray:

—————— forward + comment articles, add a small assistant.