25. Nio (non-blocking mode debugging (multiple client connections and data reading and writing))

Nio (non-blocking mode debugging (multiple client connections and data reading and writing))

 Analyze the code, here we judge if, that is, we only print when there is a client connection and the client sends data. Since it is in non-blocking mode, we delete the prints outside the loop (non-blocking mode, the thread keeps running, and it will keep printing if it doesn’t stop)

 

What we want to debug is whether a single thread can handle multiple client connections and data reading and writing in non-blocking mode

The first step: (start the server first and then start a client)

 

 

Step 2: (Start the second client, the third client)

 

 

Among them, we analyze that we are in non-blocking mode, our thread does not stop, so no matter how many clients connect, he can detect it in time (this while is constantly looping)

Step 3: (We use a client to send a data) Among them, this loop will check whether there is a connection established, and there will be a small loop to check whether there is a client sending data to this connection

 

 

Summary: Our non-blocking mode can handle multiple client connections and data reading and writing in single-threaded mode. (Multiple client requests cannot be processed in blocking mode. Because the thread stops after blocking (blocking method))

Disadvantages: This thread has been running in a loop, too busy. always running

Optimization: that is, use our soloter (we need to use it and let this thread run)

Guess you like

Origin blog.csdn.net/logtcm4/article/details/127804037