Usually Java Developers like to write programs using synchronous code , Because of this request (request) / Respond to (response) The way is simple , And more in line with the thinking habits of programmers ; It's a good way to do it , Until there is a performance bottleneck in the system . When using synchronous programming , Only one request can be initiated at the same time And wait to return synchronously , So in order to improve system performance , At this point, we need to introduce more threads to achieve parallel processing . But when accessing shared resources under multithreading , Resource contention and concurrency will inevitably be introduced ; in addition , The number of threads is limited at the operating system level , It is not possible to provide system performance by increasing the number of threads indefinitely ; and , Programming with synchronous blocking also wastes resources , For example, launching a network IO When asked , The calling thread will be in the state of synchronous blocking waiting for the response result , At this time, the calling thread can clearly do other things , Wait for the Internet IO After the response results are returned, the results are processed .
It can be seen that parallel programming by increasing the number of threads in a single machine system is not “ A ready-made panacea ”. By writing asynchronous 、 Non blocking code , You can switch execution to another active task using the same underlying resource , After the asynchronous processing is completed, it returns to the current thread to continue processing , To improve system performance .
Asynchronous programming is a way to make programs run in parallel , It allows a unit of work in the program to run independently from the main application thread , And at the end of the unit of work , The main application thread will be informed of its running result or failure reason . Using asynchronous programming can improve the performance and responsiveness of applications .
For example, when the calling thread initiates the network asynchronously IO After the request , The calling thread will not block synchronously and wait for the response result , But after the memory saves the request context , Just go back and do something else , Wait for the Internet IO The response results are returned before use IO The thread notifies the business thread that the response result has returned , The result is processed by the business thread . so , Asynchronous call mode improves the utilization of threads , Let the system have more thread resources to process more requests . For example, in mobile applications , After the user operates the mobile device screen to initiate a request , If it's synchronization, wait for the background server to return the result , When the background service operation is very time consuming , Will cause the user to see the mobile device screen to freeze ( One In the process of request processing ), Before the results return , Users cannot operate other functions of the mobile device , It's very bad for the user experience . And with asynchronous programming , When a request is made , The calling thread will return immediately , The specific return result will be passed through UI Threads render asynchronously , And in the meantime, users can use other functions of the mobile device .
Sharing this learning note today is just a piece of Java Top level tutorial notes for asynchronous programming ！
Because the notes are too much , The following can only show you part of the content , The complete Blu ray version is available for free at the end of the article ！
Detailed chapter contents
The first 1 Chapter one is about asynchronous programming
This chapter mainly introduces the concept and function of asynchronous programming ,Java in Asynchronous programming scenarios and different asynchronous programming scenarios should use what technology to achieve .
The first 2 Chapter explicitly uses threads and thread pools to implement asynchronous programming
This chapter focuses on how to explicitly use threads and thread pools to implement asynchronous programming , This includes how to explicitly use threads for asynchronous programming and the disadvantages of using thread programming , How to use thread pool to realize asynchronous programming and the principle of thread pool implementation .
The first 3 Chapter is based on JDK Medium Future Implement asynchronous programming
This chapter mainly explains how to use JDK Medium Future Implement asynchronous programming , This involves how to use FutureTask Implement asynchronous programming and its internal implementation principle ; How to use CompletableFuture Implement asynchronous programming and its internal implementation principle , as well as CompletableFuture And JDK Stream How to combine perfectly .
The first 4 Chapter Spring Asynchronous execution in the framework
stay Spring Framework Respectively use TaskExecutor and TaskScheduler Interface provides abstraction for asynchronous execution and task scheduling , In this chapter, we focus on TaskExecutor Supporting notes @Asyne How to implement asynchronous processing .
The first 5 Asynchronous programming based on reactive programming
This chapter mainly explains how to use reactive programming to realize asynchronous programming , It contains what reactive programming is , Why reactive programming is needed , What are the characteristics and values of reactive programming , And how to implement Library Based on reactive programming RxJava And Reactor Implement asynchronous programming .
The first 6 Chapter Web Servlet Asynchronous non blocking processing of
This chapter mainly discusses Servlet3.0 Pre specification synchronization model and disadvantages ,Servlet3.0 The asynchronous processing capability provided by the specification is similar to Servlet3.1 The specification provides non blocking I0 Ability , as well as Spring MVC Asynchronous processing capabilities provided in .
The first 7 Chapter Spring WebFlux Asynchronous non blocking processing of
This chapter focuses on Spring frame 5.0 The new WebFlux Technology stack , It also introduces the significance and value of its existence 、 Concurrency model and applicable scenarios 、 How to base on WebFlux Implement asynchronous programming , And its internal implementation principle .
The first 8 Chapter high performance asynchronous programming framework and middleware
This chapter mainly introduces some high performance asynchronous programming frameworks and middleware , This involves asynchrony 、 Network programming framework based on event driven Netty; High performance RPC Frame 1 Apache Dubbo; High performance inter thread messaging library 1 Disruptor; asynchronous 、 Distributed 、 Message driven framework 1 Akka; High performance distributed message framework 1 Apache RocketMQ.
The first 9 Chapter Go The asynchronous programming capabilities of language
This chapter focuses on Go The asynchronous programming capabilities of language , This includes Go The thread model of language , And how to use primitives goroutine And channel Implement asynchronous programming , Based on the goroutine And channel Build a support back pressure 、 The pipeline of asynchronous programming .
We need a blu ray version Java Friends of Asynchronous Programming Notes ：
—————— forward + Comment on article , Add a little assistant .
本文为[Jie sharing architecture]所创，转载请带上原文链接，感谢