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.

1.1 Asynchronous programming concepts and functions

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. When using synchronous programming, each thread can only initiate one request at a time and wait for the return synchronously, so to improve the 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 result of the response, when the calling thread clearly can do other things, and wait for the network IO response result to return and then 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 the 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 it Other things, such as network IO response results back before using IO thread thread inform business response Fruit 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, such as fruit is 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 () has been requested, before the results back to 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.

1.2 Asynchronous programming Scenario

In everyday development, we often encounter situations where something needs to be done asynchronously without knowing the outcome of the asynchronous task. For example, if the log is printed asynchronously in the calling thread, the log is set asynchronously so as not to block the calling thread. As shown in figure 1-1 of the asynchronous log printing, use an in-memory queue to log print asynchronous, then using a single thread consumption asynchronous processing memory log event in the queue, perform specific log Trading operations (essence is more than a single consumption model), in this case, the calling thread After put the log task in the queue will continue to perform other operations, They don’t care about when the log task was logged.

Information:

  • Java asynchronous task processing: www.cnblogs.com/chenmo-xpw/…

  • Java concurrent asynchronous programming, the original ten interface work now only need one interface

Mp.weixin.qq.com/s?src=11&ti…

You can follow my official account:

Personal Introduction:

Occupation: Senior software development engineer

City: Shanghai for 3 years, resident in Hangzhou

Planet Of Knowledge: Practice Reading Club

Writing: 7 days to write for 7 years

Personal label: programmer, founder of practice reading Club, practitioner of self-discipline: Insist on getting up at 6:00 for 7 years, post-90s happy dad, practitioner of “Intimate Relationship” : the only standard to solve problems — everyone truly feels love!