what? ? ? Qingchun elementary school girl was cried out by Taobao senior Java technical experts who took three years to produce the actual Java asynchronous programming documentation! !

Preface

This article is compiled and shared by Taobao senior java technical experts to share the actual java asynchronous programming documentation. Aiming at common asynchronous programming scenarios, it explains the principles and methods of asynchronous programming from the perspectives of programming languages ​​and development frameworks. Each technical point is accompanied by case codes. !

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.

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 infinitely 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 waiting for the response result, and 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 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 of the main application thread, and after the work unit is finished, the main application thread will be notified 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 freezing (always in the request processing 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.

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

This article will summarize the contents of the three parts from the table of contents, the main content and the article, and give you an introduction to the actual documentation of java asynchronous programming. I hope you will like it! ! !

First, you can take a look at the catalog

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

Second, let’s look at the main content

This article is written in order from simple to difficult, and each chapter has some code examples so that everyone can practice it in order to deepen understanding. If you have a certain foundation in Java concurrent programming and asynchronous programming, then you can directly view the interesting chapters from the catalog to learn. This article is divided into 9 chapters, the content is as follows:

Chapter 1 Understanding Asynchronous Programming; In this chapter, we first introduce the concept and function of asynchronous programming, so that everyone has a general understanding of asynchronous programming; then explain the asynchronous programming scene in Java, let everyone learn more about asynchronous through actual scene cases What is programming, and what technologies should be used to achieve different asynchronous programming scenarios.

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

Chapter 2 Explicitly use threads and thread pools to implement asynchronous programming; this chapter first explores the most basic way of explicitly creating threads in Java to implement asynchronous programming, and points out three problems with it; then explains the explicit use Thread pool to realize asynchronous programming, and explain the realization principle of thread pool. Although the thread pool method provides thread reuse to obtain the return value of the task, it is necessary to block the calling thread when obtaining the return value, so we will explain the JDK in the next chapter The ompletableFuture provided to solve this problem.

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

Chapter 3 Asynchronous Programming Based on Future in JDK; In this chapter, we first explain how to use FutureTask to realize asynchronous programming and its shortcomings, and then explain how Completabl Future solves its shortcomings, and how CompletableFuture JDK Stream is perfectly combined, we can see that using CompletableFuture Implementing asynchronous programming belongs to declarative programming. Under normal circumstances, we do not need to explicitly create a thread pool and submit tasks to the thread pool. This greatly reduces the burden on programmers. In addition, this chapter is mostly a practice type. I hope you can practice the examples in this chapter. Deepen understanding.

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

Chapter 4 Asynchronous Execution in the Spring Framework; In this chapter, we explained how to use @sync in the Spring framework for asynchronous processing, and how to implement it internally by proxy, and we know that using @sync to achieve asynchronous programming belongs to declarative programming. Under normal circumstances, we do not need to explicitly create a thread pool and submit tasks to the thread pool, which greatly reduces the burden on programmers, and hope that readers can look through the code for more in-depth research.

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

Chapter 5 Asynchronous programming based on reactive programming; this chapter first explains the shortcomings of traditional asynchronous programming based on CallBack and Future, and then discusses the benefits of using Reactive programming, and finally explains how to use RxJava and Reactor libraries. Implement asynchronous programming. This chapter only explains some features of Reactive programming. If you want to know more features, you can go to the official website to view and learn.

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

Chapter 6 Asynchronous non-blocking processing of Web Servlet; in this chapter we first discuss the Servlet synchronization processing model before Servlet 3.0 and its shortcomings, and then discuss the asynchronous processing capabilities provided by Servlet 3.0 and the non-blocking IO capabilities of Servlet 3.1, as well as Spring MVC Asynchronous processing capabilities provided in. After studying this chapter, you can write a demo to practice it, and add breakpoints to debug tracking at key points in order to deepen your understanding.

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

Chapter 7 Spring WebFlux asynchronous non-blocking processing; this chapter mainly explains the new asynchronous non-blocking WebFlux technology stack introduced by Spring 5.0, which exists in parallel with the Servlet technology stack. WebFlux supports asynchronous processing from the specification, and naturally supports reactive programming based on the Reactor library, and it uses a small number of fixed threads to achieve system scalability. It is believed that it will be widely used in the near future.

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

Chapter 8 High-performance asynchronous programming framework and middleware; In this chapter, we outline some high-performance asynchronous programming frameworks and middleware for everyone to expand their knowledge and use. If you are interested in a certain component, you can study it in depth.

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

Chapter 9 Asynchronous Programming Capabilities of Go Language; In this chapter, we first explain the threading model of Go language, then explain the more important concurrency primitives goroutine and channel, and finally build a pipeline based on goroutine and channel, and experience the use through examples The pipeline performs asynchronous programming and realizes the back pressure function.

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

This [Java Asynchronous Programming Practical Combat] has 287 pages, and friends who need the full version and the assistant VX can get it for free!

Finally, we summarize the whole article

Taobao senior java technical experts explode and share the actual documentation of java asynchronous programming

 

Evaluation of industry experts

  1. Asynchronous and concurrent processing capabilities have almost become a necessary capability for software systems. Modern programming languages, operating platforms and frameworks provide comprehensive support, but developers cannot expect them to solve all related problems, so they have an insight into asynchronous and concurrent technologies Insider is a required course for advanced programmers. This article provides an in-depth, systematic summary and summary of the technologies related to Java asynchronous programming. It is an excellent learning material.
  2. Asynchronous programming is a powerful tool for high-performance programs, and it is also one of the challenges faced by programmers. To write excellent asynchronous programming code, you need to have a deep understanding of the principles of asynchronous programming and a variety of asynchronous programming frameworks. Comprehensive control. This article covers all aspects of asynchronous programming. It not only has an in-depth explanation of the principles and framework, but also shows the actual code. It is the best choice for a comprehensive and systematic mastery of asynchronous programming.
  3. This article introduces the basic concepts and common scenarios of Java asynchronous programming in a simple way. It is a must-read book for advanced Java programmers.
  4. A good book that comprehensively analyzes asynchronous programming, from JDK Future to Reactor. From Netty to Disruptor, supplemented by case studies, is rich in content and worth reading.
  5. This article explains how to perform asynchronous programming from the dimensions of programming frameworks and programming languages, such as frameworks such as SpringWebFlux and Dubbo, and languages ​​such as Java and Go. I believe that after reading this book, you can master asynchronous programming skills in an all-round and three-dimensional manner.

I hope this article can help you learn, continuously improve your technical depth and breadth, and make yourself more valuable. I also hope that this article can be liked by everyone!

Struggle and strive to achieve a better self! !

Guess you like

Origin blog.csdn.net/GYHYCX/article/details/109096937