21. Nio blocking mode

Nio blocking mode

Network programming (blocking/non-blocking)

 

Blocking: What is blocking (blocking thread) When there is no data to read, the thread must block and wait (that is, the thread stops) (blocking Think about the state of the thread, there is a blocking state (note that blocking is a state, And suspending or sleeping is a kind of behavior)) When there is data, the thread will come back to life immediately

Non-blocking: (the thread will not stop), synchronization, the thread will not stop, just let the thread perform a queue and lock

Code demonstration: (nio understands blocking mode (single thread is used here))

Service-Terminal:

  1. Created a server (ServerSocketChannel)

 

  1. Binding listener interface (bind)

 

  1. Set up a collection of connections with clients (a collection of channels)

 

  1. accpet (the server establishes a connection with the client, such as the three-way handshake of tcp)

 

  1. Receive data sent by the client

 

client:

 

Summarize:

 

Guess you like

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