NIO different processes and with the BIO

NIO:

Synchronous non-blocking, Server implementation modeA plurality of processing requests thread, I.e., the connection request sent by the client areRegistration to the multiplexerA multiplexer connected to the polling IO request is processed.
Multiple number of connections, and the connection is short, a chat room, barrage system
NIO isJDK1.4 JAVA provides a series of new features improved input / output, known collectively as the NIO, the IO synchronous non-blocking, related classes are within the package java.nio

NIO features:

NIO has three core components:
Channel (pipe), Buffer (buffer), selector (selector )
1.NIO oriented buffer, or block-oriented programming, data is read into a buffer for later processing of his (Buffer), is movable back and forth in the buffer as needed, which increases the flexibility of the process , it can provide the use of highly scalable non-blocking network.
2.Java NIO non-blocking mode, so that a transmission request from a thread or read data channel, but it can only get data currently available, if the data is not currently available, it does not get anything, rather than remaining threads blocked before it can be read until the data becomes, the thread can continue to do other things, non-blocking write is true, a thread requests to write some data to a channel, but do not need to wait for it to completely write this thread work you can do something else
3.NIO can be done with a thread to handle multiple operations. Suppose there are over 10,000 requests, according to the actual situation, 50 or 100 may be assigned to process threads, unlike the previous IO blocking as have to allocate 10,000
4.HTTP2.0 using multiplexing techniques, so that the same the number of concurrent connections a plurality of requests, concurrent requests and several orders of magnitude bigger than the HTTP1.0

Based on the above characteristics, we can conclude that the difference between BIO and NIO

1.BIO streamed data processing, and in a manner NIO data processing block, block I / O efficiency / higher efficiency than many I flows of O
2.BIO is blocked, it is non-blocking NIO
3.BIO based byte stream and character stream operations, and operates based NIO Channer and buffer, data is always read from the channel to the buffer, from or written to the buffer in the channel, a plurality of channels for monitoring events Selector ( such a connection request, data arrives, etc.), and therefore can be monitored using a single thread multiple client channel
The following is a flowchart showing the NIO:
Here Insert Picture Description
The relation can be seen:
1. Each will correspond to a Channel Buffer (buffer or array).
2.Selector one thread, a thread corresponding to the plurality Channel.
3. This figure reflects two registered to Channel Selector, which switches to Channel, is an event decision, the event is a very important concept
4.Selector based on different events, each channel in the switching
5.Buffer is a memory block, the bottom layer is an array, the write data is read by Buffer, and this BIO has a different nature, BIO or number input stream or output stream, not two-way, but the NIO was BUffer can read can write but need to cut
6.Channel is bi-directional, you can return to the underlying operating system conditions, such as Linux , the underlying operating system is a two-way channel

Lack of study time, too shallow knowledge, that's wrong, please forgive me.

There are 10 kinds of people in the world, one is to understand binary, one is do not understand binary.

Published 71 original articles · won praise 54 · views 420 000 +

Guess you like

Origin blog.csdn.net/weixin_43326401/article/details/104138419