Three IO modes from synchronous blocking chat to Java

Source: Internet
Author: User
Tags ming

This paper summarizes the answer from https://zhuanlan.zhihu.com/p/34408883,https://www.zhihu.com/question/19732473, http://blog.51cto.com /stevex/1284437 Author's own comments   Quick to understandFirst of all, we must first have a visual understanding of these concepts, for beginners, you can look at these concepts:
    • blocking non-blocking refers to the client
      • blocking : means that after a request is made by the client, it can only wait until the response is received
      • non-blocking : means that after a request is made by the client, the client can do other things before it gets a response
    • Synchronous Asynchronous refers to a server-side
      • synchronization : means that the server accepts a request and cannot do anything else until the result is returned
      • Async : means that after a request is accepted by the server, although no results have been returned, other things can be done
This kind of understanding is actually too partial, because this is only the explanation of the message notification scenario, but by substituting the client, the server side is more convenient for beginners to understand, so here, for the moment to explain this. As An example ,Xiao Ming led his girlfriend to the supermarket shopping, bought a lot of things, when he went to the cashier to checkout, Xiaoming ( Client) issued a request for a checkout message ( Request), Cashier ( Server) will take care of his request. It's possible to produce multiple scenarios at this time
    • Xiao Ming foolishly waits for the cashier to calculate the total price of all items with a calculator and prepares to pay. ( synchronous blocking : xiaoming has been waiting before requesting a response; The cashier has not done anything else and has been dealing with Xiaoming's request )
    • Xiao Ming thought he was too stupid, so he chatted with his girlfriend, urging the cashier to quickly calculate the total price. ( synchronous non-blocking : Xiaoming did something else before requesting a response; The cashier has been dealing with Xiaoming's request )
    • Xiao Ming innocently waiting for the cashier's total price results, the cashier but the calculation of the work to the computer and then go to the bag to help install things, until the computer appeared on the total results, the cashier continued to return to complete the collection work. ( Asynchronous blocking : Xiaoming waits before requesting a response; The cashier handles other things during the process of processing Xiaoming's request )
    • Xiao Ming felt that he was too stupid, so while chatting with his girlfriend, while urging the cashier to quickly calculate the total price, while the cashier to the computer after the calculation of the work to take the bag to help put things, until the computer appeared on the total results, the cashier to continue to complete the collection work. ( asynchronous non-blocking : Xiaoming does other things before requesting a response; The cashier also handles other things during the request process )
In this example:
    • blocking, non-blocking , refers to whether Xiao Ming is waiting to process the results of the process to do other things.
    • synchronous, asynchronous , refers to whether the cashier has done other things in the process of processing a request for payment, which also led to the receipt of the result of a notification to xiaoming, or after the additional notice.
Another example of Lao Zhang Love tea, nonsense do not say, boiled water. Appearance characters: Lao Zhang, Kettle Two (ordinary kettle, referred to as a water bottle, the kettle will ring, referred to as the kettle).
    1. Lao Zhang put the kettle on the fire and conforming the water. ( synchronous blocking ) Lao Zhang thinks he's a little silly.
    2. Lao Zhang put the kettle on the fire, went to the living room to watch TV, and occasionally went to the kitchen to see if the water was open. ( synchronous non-blocking ) Lao Zhang still feel a little silly, so become high-end, bought a will sound flute of that kind of kettle. After the water is open, you can make a loud noise.
    3. Lao Zhang put the kettle on the fire and conforming the water. ( asynchronous blocking ) Old Zhang thought such a silly meaning is not very important
    4. Lao Zhang put the kettle on the fire, went to the living room to watch TV, the kettle rang no longer to see it, rang again to get the pot. ( asynchronous non-blocking )
In this example:
    • The so-called synchronous asynchronous , just for the kettle.
      • Ordinary kettle, synchronous; kettle, asynchronous.
      • Although all can work, but the kettle can be completed after their own, the old Zhang Water opened. This is not an ordinary kettle.
      • Synchronization can only let the caller old Zhang to poll themselves (in case 2), resulting in the low efficiency of the old Zhang.
    • The so-called blocking non-blocking , only for the old Zhang.
      • The old Zhang of conforming, blocking; Old Zhang watching TV, non-blocking.
      • Situation 1 and situation 3 old Zhang is blocked, the daughter-in-law shouted he did not know. Although the 3-ring kettle is asynchronous, it does not make much sense to conforming's old Zhang.
asynchronous is generally used in conjunction with non-blocking, in order to play an asynchronous effect。   Conclusion: blocking and non-blocking: The concern is whether the caller will be waiting for the caller's notification. In other words, whether the requester will do something else in the process of waiting. Synchronous and asynchronous: The concern is whether the callee will continue to process the request. In other words, processing the requester is the process of continuously processing the request and returning the result through the original call, or it can handle other things before the request is processed, and notifies the caller by other means when the request is complete. Deep DiveAn IO operation is actually divided into two steps: initiating an IO request and the actual IO operation. blocking IO and non-blocking IOThe difference iswhether the initiating IO request will be blocked, if blocking until completed then is the traditional blocking IO, if not blocked, then non-blocking IO. Synchronous IO and asynchronous IOThe difference iswhether the actual IO operation is blocking, if the actual IO read-write blocking request process, then is synchronous IO. blocking and non-blockingis for the process to access the data at the time, according to the readiness of the IO request state to take a different way, plainly speaking is a read or write operation function Implementation mode, BlockingThe read or write function will wait until the non-blockingmode, the Read or write function returns a status value immediately. Synchronous and asynchronousis for the interaction of the application and the kernel, SyncRefers to a user process that triggers an IO operation and waits or polls (when polling does not do anything else) to see if the IO operation is ready and Asynchronousis when the user process triggers an IO operation and then starts to do its own thing, and when the IO operation is completed, it is notified by the IO completion. Therefore, IO operations can be divided into 3 categories: Synchronous Blocking(that is, early IO operations, BIO)、 synchronous non-blockingNIO)、 asynchronous non-blockingAIO)。
    • Synchronous blocking : In this way, the user process must wait for the IO request to return after initiating an IO request, and the user process can continue to run only after the IO request is returned. Java's traditional IO model (BIO) belongs to this approach.
    • synchronous non-blocking : In this way, the user process initiates an IO request can be returned to do other things, but the user process needs to ask from time to time the IO operation is ready, which requires the user process to constantly ask, so as to introduce unnecessary waste of CPU resources. The current Java NIO belongs to synchronous non-blocking IO.
    • asynchronous non-blocking : After the user process initiates an IO request, it returns to do other things, and the application that really handles IO initiates an IO operation, does not wait for kernel IO operations to complete, and can handle other things, and so on, when the kernel completes IO operations, it notifies the application of processing IO. Applications that process IO then notify the user of the process. Currently, AIO in Java belongs to asynchronous nonblocking io.
  History of Java three IO BIO: Before JDK1.4 NIO: JDK1.4 launched the Java.nio package AIO: JDK1.7 added four asynchronous channels under Java.nio.channels package
    1. Asynchronoussocketchannel
    2. Asynchronousserversocketchannel
    3. Asynchronousfilechannel
    4. Asynchronousdatagramchannel

Three IO modes from synchronous blocking chat to Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.