48. Nio (Io model (asynchronous (asynchronous and synchronous)))

Nio (Io model (asynchronous (asynchronous and synchronous)))

  1. Synchronization: the thread gets the result by itself (one thread)
  2. Asynchronous: The thread does not get the result by itself, but the thread gives you the result (at least two threads)

 

! ! ! Scenario: (Complete a data exchange)

The phone calls are synchronous: What is data exchange, it is called communication once it comes and goes. To make sure that answering the phone is to get the result (to complete the communication), here is a thread.

WeChat chat is asynchronous: a person who sends a message is a thread, and we don't need to determine whether to receive the message or not. We reply to the message to complete the data exchange (two threads)

! ! ! ! We are here to see whether the previous blocking/non-blocking/multiplexing is synchronous or asynchronous. For example, if blocking is synchronous, it is synchronous blocking.

The first step: synchronous blocking (blocking io is synchronous), it is its own thread that initiates the read, and after the data is copied, it is still given to the previous thread (the result is synchronous because of its own result)

 

The second step: synchronous non-blocking (non-blocking io is synchronous), and finally after copying the data, it still gives us the user thread. (Get the result yourself, it is synchronous)

 

Step 3: Multiplexing is still synchronous (for example, read, the user thread finally gets the result, so it is still synchronous)

 

Step 4: Asynchronous IO

1. After Read, a thread returns directly (without returning a result) with a callback method (parameter), and another thread calls the callback method of the first thread (returns the real result and passes it back as a parameter).

2,! ! Asynchronous is not blocking, as long as asynchronous non-blocking

 

Je suppose que tu aimes

Origine blog.csdn.net/logtcm4/article/details/127851445
conseillé
Classement