NIO road 1-- traditional BIO to the evolution of the NIO

A, BIO era

BIO1.0 version

Before jdk1.4, BIO java network programming mode is used, BIO stands for Blocking IO, which is blocking IO on to the next call 10086 customer service phone as an example, how to communicate at BIO. (10086 customer service is the service side, the mobile user is the client and 10086 is the port number) BIO1.0 version customer waits for the user to make calls 10086, and this was blocked because the customer can not do anything else, can only wait for customers to call. When users dial 10086 and customer service phone is turned on exchanges between customers and customer service. Until the end of the exchange, the customer hang up, then call continues to wait for the next call.

Question: When, after the first customer after switching 10086 telephones, being the exchange, if the second customer also dialed 10086, you will be prompted to being busy until the first customer hung up the phone before you can turn on .

BIO2.0 version

After the primary customer waits for the user to dial 10086, the user dials the phone 1, the user will call a special adapter to the processing demands of the customer client A (new thread processing a client request), then the user 2 can call 10086 to get through, and then Support primary user 2 to transfer the customer B, the user 3 to transfer the customer C, and that the 10086 can not be busy.

Question: When under high concurrency, at the same time there are 10,000 users dial 10086, then you need to arrange for 10,000 customer service to handle user demands, and there is no upper limit, it is easy to lead to customer service resources are not enough.

BIO3.0 version

Founded mobile customer service department, customer service staff set an upper limit of 100 (create a thread pool, the upper limit is 100 threads), then when users dial 10086, the main customer service or customer arrangements A, Customer B, Customer C to handle user demands when the 100 customer service in handling the demands of the user, the first 101 users dial 10086, the main customer service will notify the customer service is busy, please wait. . . After 100 becomes idle after a busy customer service there is a matter before a customer has been processed, the processing immediately come to the first 101 user demands. This avoids the situation where the customer service is not enough (the number of threads to be controlled)

Question: In the case of high concurrency, avoiding the problem of not enough resources, but does not address user needs to wait too much from the fundamental

Summary: The main characteristics of BIO is a user requires a customer to deal with, which is a client needs to allocate a thread to handle, that is, the ratio between the client and the number of threads is always 1: 1 relationship. If the excessive number of concurrent users, or customer service process is slow, it will lead to other clients has been in a wait.

Era ending BIO

Two, NIO era

With the increasing number of users, the company felt the move using BIO operating mode can not even solve the needs of users, and began to reform, namely to reform the following questions. One problem: After BIO era, after the user dials 10086, the main customer service will transfer the user to handle customer transactions (assigned a thread to handle client requests), but it is possible that the user has been dialed telephone stood without speaking, it did not sound good analogy is that dog in the manger, so if a user assigned to handle customer service, customer service is clearly a waste of resources.

A reform: for the above problem, moving enacted regulations, only when the user throws a certain appeal, was assigned to deal with customer service (when there is certain IO operations between the client and server create a new thread to handle). Such an approach can filter out what is the real demands of the user, which is an invalid user. To achieve such a measure, we need to deal with customer service there from the Lord, which is the main customer took out a small notebook, which recorded above the user is a valid user, which is not a valid user. (Use of map records, key client, value is the current state, and this little book is in NIO Selector)

Second problem: When a user sends demands, customer service during processing, and customer service users are blocked, because they are not capable of other things, users need to wait for the customer service demands finished, the user needs to wait for the customer to obtain processed results. If the user is a stutter or a sloth (client network delay) or if too many users demands (more data is sent to the server), then the customer service at this time must have been waiting for; the same token, if the customer service processing capability slow (poor CPU performance) or a more complex business process (server more complex business logic), then the time the user has to wait. Press a classic lines "Shaolin Soccer" in Stephen Chow said: "I have hundreds of thousands of a second up and down it," Obviously, the current way of working time costs will be relatively large.

Reform II: For the above problem, moving enacted regulations that set the micro-channel public number to handle user demands, the user of the problem with the voice (there will be problems in speech) to describe their need to be addressed in the complaint micro-channel public number, just after escalation We can go to the shop and go to something else (non-blocking) without the need to wait for customer service deal with the problem; after a while after the call to obtain the user's demands through public micro-channel number the background, you can handle user demands, processing is completed, the processing results through the micro-channel public number sent to the user, then the customer service you can do other things to go to the (non-blocking), while the user is idle and when they can view the results of public micro-channel number above. (NIO voice will be packaged in a buffer Buffer, the micro-channel public number packaged in a Channel) BIO era, the company is moving to communicate by phone and customers, NIO era through the micro-channel public number of sending and receiving voice and customer communications. BIO age, server and client are interactively oriented stream; the NIO age and sending and receiving buffer through client interaction channel.

These are generally from BIO to NIO evolution then again summarize a few concepts: synchronization IO: IO operation initiated when the client, if the server is not executed, then the client needs to have waited until the results of asynchronous IO: When client initiated IO operation, you can shop and go to something else immediately, after the completion of the implementation of the server will notify the client gets results blocking IO: when the IO operation, if the read and write IO is not completed, then the result will not return but blocked behind the current thread or process operations non-blocking IO: IO operations only when, if the read and write IO is also not completed immediately return a result do not need to wait to read and write IO operations will be IO IO multiplexing by: IO is network IO, multi-channel refers to multiple TCP connections or multiple channel, multiplexing refers to one or a small number of threads to handle these TCP connections or channel, which is more than one network IO multiplexing one or a small thread to handle

BIO is a synchronous blocking IO, because the client needs to wait for the service end result, so are synchronized; in the absence of server IO operation has been processed, the result does not return immediately, but blocking is synchronous non-blocking IO NIO, because the client the results still need to wait for the server, the synchronization; and when the server is processing the IO buffer are processed by the transmission and reception of data is carried out through buffer, rather than have to wait to read and write IO stream, the non- blocked

And after the NIO has emerged NIO2.0 version is AIO, AIO is asynchronous non-blocking IO, server implementation model is a valid request is assigned a thread. Client I / O requests are completed by the OS first and then inform the server application to start a thread for processing.

Three, AIO era

NIO AIO is version 2.0, it is to achieve non-blocking asynchronous IO

Fourth, the operating system IO multiplexing achieve

Currently supports I / O multiplexing system using operating select, poll or epoll of ways, corresponding to a plurality of process monitoring FD (Socket descriptor), once a ready FD (read or written), a corresponding operation program can be notified.

select mode

1.select is a single-process monitoring of file descriptors, limited number of file descriptors, set in FD_SETSIZE, usually 1024 or 2048

2.select socket to scan the full scan time is linear, based on polling, less efficient

3. Copy the need to maintain a data structure to store large amounts of FD, it requires from the user space and kernel space in the data transfer overhead structure Grand

poll mode

1.poll way linked list stored FD, FD no limit on the number, and select other almost the same

epoll way

FD no limit to the number 1.epoll way

2.epoll using a plurality of descriptors managed file descriptor.

3. Using Event Notification way through epoll_ctl registration fd, fd, once ready, the kernel uses a similar callback callback to activate the fd, epoll_wait can be notified

4. If the socket are all active, then the performance epoll general, the reality is that a larger number of descriptors managed, and less active.

Fifth, the realization of the principle of NIO

NIO are three core channel, selector and buffer, then the first review under the NIO workflow:

Server:

1, create a ServerSocketChannel, so the client channel is the parent channel, for connecting the monitor client

2, binding the server port, set the mode to non-blocking mode

3, create a multiplexing channel manager Selector (only one or a few high Selector can handle concurrent channel)

4, the ServerSocketChannel registered to Selector, and listen ACCEPT event (client connection event)

5, Selector to find an infinite loop by all registered channel has no corresponding event trigger

6, when listening to the event for client connections, create SocketChannel, and the client TCP connection, and is set to non-blocking mode

7, and the SocketChannel between the client registered to the Selector, and listen OP_READ event (channel-readable event, write data to the client, the server can read the data)

8, create a buffer of a certain size, the buffer will be sent to the client data channel, and analyzes

9, the read data into the system for processing traffic

10, write back data into the buffer and calls the write method of channel (do not need to close the channel, channel the client is disconnected after the connection, the server will receive ON_READ event, then you know the channel error disconnected)

BIO and NIO biggest difference is the introduction to the channel, selecotr and buffer to the concept, these three components to the principle is kind of how to do? Let's take your time.

buffer (Buffer):

Buffer to read and write to the cache effect is all the data, the array position in the array implementation tagging buffer data is located, such as a byte array with the data bytes, with the character char array variable mark position and the like have a plurality of, namely, capacity, position, limit and mark

capacity is the capacity, the fixed size of the array, the initialization settings

position indicates the current position, the initial value is 0. When writing, write each bit of data in the buffer, it moves back one position in the range of 0 ~ capation-1, when the buffer switching from write mode to read mode, position reset to 0 when the data is read from the buffer, each bit of data is read, it moves back one position

limit indicates how much the current can read or write data in the write mode, the default value capation, when the value is set to position 0, position limit value

mark indicates a note position, corresponding to a marker. Mark method was called to set the mark = position, call the reset setting position = mark

The relationship between the four attributes 0 <= mark <= position <= limit <= capacity

Here illustrates read and write data buffer array

First, initialization initializes a capacity of the array 10, then capacity = 10, position = 0, limit = 10, mark uninitialized

Now the data written from the buffer array, call put () following method, assuming the data write size 5, the writing capacity, limit values and the same mark, position becomes 5, the array represents the position labeled 5 new data can be written, as shown

When the buffer needs to be read out, the call Flip () method, shows a switching current buffer from the write mode to the read mode, when the start position is reset to 0 indicates 0 is read from the position of the limit to 5 5 represents the location just written to, mark and the capacity value remains unchanged, as shown

Each read data, position data will be read with the move, but if the data is read to find half the problems need to re-read it again from scratch, then you need to call r ewind () method will be reset to position 0 the other three values unchanged

另一种情况,假设读取了一部分数据之后就不再读了,而是再切换会写模式,此时如果之间从5到位置开始写,显然已经被读到空间就无法再写入数据了,会造成空间的浪费,此时就可以调用compact()进行压缩处理

compact方法的逻辑是将当前剩下的未读数据复制到0的位置,limit重新设置为最大值,position设置为下一个可写到地址,如上例,如果读出了“AB”两个数据,还剩下“CDE”未读,那么执行compact方法之后的结果如下图示:

上图中可以看出“DE”两个数据会重复,但是再下一次执行put读时候会被新数据所覆盖,position指向了下一个可以写到位置

channel(通道)

selecotr(多路复用选择器)

Guess you like

Origin www.cnblogs.com/jackion5/p/11012867.html