Another pattern that corresponds to the blocking mode is called non-blockingIOmode, the read and write operations are not blocked during the entire communication process, and there is no blocking condition for the current processing thread. FromAMachine toBthe communication process of the machine is:AMachine One thread sets the channel to write events and then executes, while another thread traverses to this channel with bytes to write andSocketWrite data,BMachine A thread traverses to this channel with bytes to read, to another thread toSocketread the data, processing and then set the channel to write events, traverse thread traversal to this channel has bytes to write, and go toSocketwrite numbers are rumored toAThe machine continues to loop this operation until communication is complete. This process has two main types of threads for each machine, one for logical processing and the other for the threads that change the channel to writable or readable events, and the other is dedicated to traversing the channel and responsible forSocketRead and write threads, this way is non-blockingIOmode.
In blockingIOmode, there is a service-side socketServerSocketfor the receiving client to connect inSocket, whether it's blocking or non-blockingIOUltimately, we need to getSocketTo Read and write operations, corresponding to blocking mode, non-blocking mode for receiving clientsSocketthe object isServersocketchannel, in addition, the blocking mode is used directlySocketobject for read-write operations, while non-blocking mode uses theSocketchannelobject for read-write operations, butSocketchannelIn essence, it is ultimately throughSocketRead and write, just read or write when the buffer concept is introduced. Finally, there is a very important object is the selectorSelector, it provides for allChannelThe filtering function of various events of interest, that is, what channels need to be handled by the selection.
Down to the principle of non-blocking mode implementation, for example,ServersocketchannelcalledOpen ()method initializes the package inside theSocketServices and willServersocketchannelinop_acceptEvent Registration toSelectorwhile the operating system createsSocketunderlying data structures and listening to clientsSocketconnection, the operating system is uniformly placed in a queue for client connection maintenance. It is then important that the application-level polling operations continue to executeSelectorretrieves an event of interest, if there are exactly three clientsSocketeven in,Selectorchoose out of threeop_acceptevent, callingserversocketchannel.accept ()receive three client channelsSocketchannelobject, and then the three client channels toOp_read,Op_writeRegister toSelectorread-write operations in the back, and down ifSelectorTraverse outOp_readorOp_writeevent can be used for the correspondingChannelRead and write operations are in progress.
Selectorplay the most important role in it and see how it completes the filtering of the events of interest. For example, the middleSelectoris its general structure, maintaining theRegisteredkeys,Selectedkeys,Cancelledkeysthree sets, and one more.Channelwith theKeythe table that corresponds to the relationship, andKeyThe event set of interest is includedInterestopsand the set of events that are readyReadyops. WhichRegisteredkeysStore Register toSelectorof allKey, whileSelectedkeyswhich is the selectedKey, it is detectedRegisteredkeysinKeythe event of interest occurs after the depositKeythe place,Cancelledkeysyou have already called theCancel ()method to be counter-registeredKey. When the application layerSelectorconstantly callingSelect ()method is first based on theCancelledkeysto removeRegisteredkeysand theSelectedkeyscorresponding to theKeyto cancel the correspondingKey, and then indirectly invoke the operating system to do the OS-levelSelect, once thereRegisteredkeysevents of interest will correspond to theKeyAdd toSelectedkeysin, such asSelectedkeysalready existsKey, the event is added to theKeyThe set of prepared events inReadyopsthe. After this operation, when the application layer invokes theSelectorof theSelectedkeys ()then take the selectedKeycollection, which can then be obtained to the correspondingChannel, depending on the eventChannelto operate.
Understanding the principle of nonblocking io mode helps to model the network IO in real -world scenarios , typically using non-blocking NIO in high concurrency scenarios where multiple connections are simultaneously required mode, which maintains the connection through one or a few threads, and the specific read-write and logical processing to other threads, greatly improving the machine's utilization rate, crushing machine CPU . In the case of blocking io mode, it is possible for threads to block on io and cause low machine usage.
Students interested in Java can exchange the following:
Non-blocking IO mode principle