Generally, Java developers like to use synchronous code to write programs, because this request/response method is relatively simple and more in line with programmers' thinking habits; this approach is good until the system has a performance bottleneck. When using synchronous programming, since each thread can only initiate one request at the same time and wait for the return synchronously, in order to improve system performance, we need to introduce more threads to achieve parallel processing at this time. However, when accessing shared resources under multi-threading, resource contention and concurrency problems will inevitably be introduced; in addition, the number of threads is limited at the operating system level, and it is impossible to increase the number of threads indefinitely to provide system performance; and , The use of synchronous blocking programming will also waste resources. For example, when a network IO request is initiated, the calling thread will be in a state of synchronous blocking and waiting for the response result. At this time, the calling thread can obviously do other things, and wait for the network IO response result to return. Process the results later.
It can be seen that parallel programming by increasing the number of threads in a stand-alone system is not a "magic bullet". By writing asynchronous, non-blocking code, you can use the same underlying resources to switch the execution to another active task, and then return to the current thread to continue processing after the asynchronous processing is completed, thereby improving system performance.
Asynchronous programming is a method that allows programs to run in parallel. It allows a unit of work in the program to run independently from the main application thread, and after the work unit runs, it will notify the main application thread of its running result or Reason for failure. Using asynchronous programming can improve the performance and responsiveness of the application.
For example, when the calling thread initiates a network IO request in an asynchronous manner, the calling thread will not synchronously block and wait for the response result, but after the request context is stored in the memory, it will immediately return to do other things, and then use IO after the network IO response result is returned. The thread notifies the business thread that the response result has been returned, and the business thread processes the result. It can be seen that the asynchronous call method improves the utilization of threads, allowing the system to have more thread resources to handle more requests. For example, in a mobile application, after the user operates the mobile device screen to initiate a request, if it is synchronously waiting for the background server to return the result, when the background service operation is very time-consuming, it will cause the user to see the mobile device screen freeze (always in the request In), the user cannot operate other functions of the mobile device before the result is returned, which is very bad for the user experience. When using asynchronous programming, when a request is initiated, the calling thread will return immediately, and the specific return result will be rendered asynchronously through the UI thread, and the user can use other functions of the mobile device during this period.
This study note shared today is a top tutorial note on Java asynchronous programming!
Due to the excessive content of the notes, only part of the content can be shown below for everyone. The free way to obtain the full Blu-ray version is at the end of the article!
Detailed chapter list
Chapter 1 Understanding Asynchronous Programming
This chapter mainly introduces the concept and function of asynchronous programming, the scenarios of asynchronous programming in Java, and what technologies should be used to implement different asynchronous programming scenarios.
Chapter 2 Explicit use of threads and thread pools to achieve asynchronous programming
This chapter mainly discusses how to explicitly use threads and thread pools to achieve asynchronous programming. This includes how to explicitly use threads to achieve asynchronous programming and the shortcomings of using thread programming, how to explicitly use thread pools to achieve asynchronous programming and the principle of thread pool implementation.
Chapter 3 Implementation of Asynchronous Programming Based on Future in JDK
This chapter mainly explains how to use Future in JDK to implement asynchronous programming, which includes how to use FutureTask to implement asynchronous programming and its internal implementation principles; how to use CompletableFuture to implement asynchronous programming and its internal implementation principles, and how to combine CompletableFuture and JDK Stream perfectly.
Chapter 4 Asynchronous Execution in the Spring Framework
In the Spring Framework, the TaskExecutor and TaskScheduler interfaces are used to provide abstractions of asynchronous execution and task scheduling. In this chapter, we focus on how to implement asynchronous processing based on the annotation @Asyne supported by TaskExecutor.
Chapter 5 Asynchronous Programming Based on Reactive Programming
This chapter mainly explains how to use reactive programming to achieve asynchronous programming, what it contains is reactive programming, why reactive programming is needed, what are the characteristics and values of reactive programming, and how to realize asynchronous programming based on reactive programming implementation libraries RxJava and Reactor .
Chapter 6 Asynchronous non-blocking processing of Web Servlet
This chapter mainly discusses the synchronous processing model and shortcomings before the Servlet3.0 specification, the asynchronous processing capability provided by the Servlet3.0 specification, the non-blocking I0 capability provided by the Servlet3.1 specification, and the asynchronous processing capability provided in Spring MVC.
Chapter 7 Asynchronous non-blocking processing of Spring WebFlux
This chapter mainly explains the new WebFlux technology stack introduced in Spring Framework 5.0, and introduces its value and significance, concurrency model and applicable scenarios, how to implement asynchronous programming based on WebFlux, and its internal implementation principles.
Chapter 8 High-performance asynchronous programming framework and middleware
This chapter mainly introduces some high-performance asynchronous programming frameworks and middleware, including asynchronous, event-driven network programming framework-Netty; high-performance RPC framework-Apache Dubbo; high-performance inter-thread messaging library-Disruptor; asynchronous, distributed, Based on the message-driven framework-Akka; high-performance distributed messaging framework-Apache RocketMQ.
Chapter 9 Asynchronous Programming Capabilities of Go Language
This chapter mainly explains the asynchronous programming capabilities of the Go language. This includes the threading model of the Go language and how to use the primitive goroutine and channel to implement asynchronous programming. Finally, a pipeline supporting back pressure and asynchronous programming is constructed based on the goroutine and channel.
Those who need the Blu-ray version of Java asynchronous programming notes:
Repost + comment on the article, just add a small assistant.