Knowledge of java in 10 minutes-NIO and java in 10 minutes

Source: Internet
Author: User

Knowledge of java in 10 minutes-NIO and java in 10 minutes
I. Introduction

Nio is a very important part of java's IO framework. Its core is to provide non-blocking IO processing methods. The most typical application scenario is to process network connections. Many people can talk about nio one or two, but they often start to endorse the principles and thoughts behind it. Many of them are not really familiar with it. I have read a lot of books and blogs, but most of them only talk about the three sets and how to use them. I seldom talk about the ideas behind them in detail, let's take a look at it this time.
Many blogs describe nio as follows: multi-channel non-blocking high-performance network IO implemented based on the Reactor mode. Then we will analyze the definition. There are two key points: multi-channel non-blocking and Reactor mode. (I wanted to calculate the high performance, but later I thought this should be the result of the first two.) Let's understand the two parts respectively.

Ii. Network I/O model

Multi-channel non-blocking is actually called the I/O multiplexing model. It is one of the five Network Models of linux and is also one of the most commonly used network programming models. For details, refer to the blog: Analysis of High-performance I/O models (this section only shows 4, with no signal-driven I/O, but I like it very much, especially the figure ), here is a brief introduction and comparison:

  • Blocking IO: this is the old java bio mode. The program needs to block and wait before receiving an event (Data arrival, data copy completion, etc. The advantage is that the encoding is simple, but the disadvantage is that the efficiency is low, and the cpu utilization is low due to the obstruction of the processing program.
  • Non-blocking IO: the handler keeps actively polling when the event is not received, so that the handler does not need to be blocked and can perform other tasks in the polling interval, but the polling will result in repeated requests, which is also a waste of resources. The pseudo-asynchronous mode previously implemented in java adopts this idea.
  • IO Reuse Model: added the socket event listener (selector) to decouple the processing program from the corresponding socket event. The socket connections used are registered in the listener, in the waiting phase, only the listener will be blocked. The processing thread can obtain the event from the listener and process the socket connection, in addition, a processing thread can correspond to multiple connections (the first two are usually a socket connection to start a thread, Which is why reuse is called), which is a bit of resource saving, since the handler can be reused by multiple connections, a few threads can process a large number of connections. The disadvantage is also the reuse. If a large number of time-consuming connections (such as a large number of connections upload large files) are involved, it is easy to cause the new connection to fail because the thread is full.
  • Signal-driven I/O model: there is no need to block the data in the quasi-phase. You only need to register a signal with the system. After the data is ready, the system will respond to the signal. This model depends on system implementation, and it is difficult to use signal communication. Therefore, there is no corresponding implementation in java.
  • Asynchronous IO: similar to signal-driven I/O, and in the data copy phase (exponential data is copied from the system buffer to the program's own buffer, and programs in other model change phases need to block the wait) it can also be processed asynchronously. Needless to say, the efficiency is very high. The disadvantage is that it depends on the underlying implementation of the system. Currently, many languages provide the implementation of this model. After jdk1.7, it is also provided in the concurrent package.

Compared with the above five models, I/O reuse models are currently a good choice in terms of efficiency and implementation costs. This is the root cause for java to implement nio based on this model. The above mentioned the implementation idea of IO reuse model. In fact, this idea has already been implemented in other languages (for example, in C ++, it is said that the trend is rampant over 10 million lines of code in the ACE, self-adapted to the communication environment, this model is used), and a design model called Reactor is proposed.

Iii. Reactor Mode

The Reactor mode, translated as the anti-cited mode, is used to separate and schedule a request to an application in an event-driven application. I believe that most people do not understand the meaning of the previous sentence (the book still needs to be backed up). To put it bluntly, it is for multiple events of a single request (such as connection and read/write ), the processing in this mode can be distinguished and handed to the corresponding processing module for processing. Let's take a look at the following diagram:

We can see that the components in Reactor mode include acceptor, dispatcher, and handler (Here we just use an example to illustrate the actual implementation.) The acceptor registers various events, when a new event is connected, the event is handed over to the dispatcher for distribution. The dispatcher binds the ing between the event and the corresponding handler, when a new event is received, it will distribute the event to the corresponding handler. handler is responsible for handling the corresponding event, which is our business layer. From this mode, we can find that for acceptor and dispatcher, we usually only need one thread as the entry, because it does not have time-consuming processing and high efficiency, handler can set up several threads as needed (usually using a thread pool), which is exactly what the IO Reuse Model expects. Next we will introduce how NIO implements this mode. Before that, we will introduce the framework. In addition to NIO, there are other Reactor frameworks implemented based on JVM, recently, OSC took the lead in translating the corresponding documents. If you are interested, please refer to the Reactor guide. IV. The details of NIO will not be discussed in detail. Here we will only introduce the next three sets:
  • Channel: pipeline, which can be seen as the encapsulation of streaming, a bit like pipe, but it is full-duplex. The advantage is that it shields the underlying details, does not care about the file or network corresponding to the stream, and does not care about how the connection is processed. In addition, the full duplex mode does not need to consider the input stream or the output stream, you only need to use buffer to read and write it.
  • Buffer: a good baseline for a channel. The bottom layer is a byte array. The difference is that it is encapsulated and not only supports basic types, in addition, the internal read and write locations (postion, limit, capacity, mark, etc.) are maintained, and convenient methods (clear and flip) are provided ). The read and write operations on the channel must pass through the buffer.
  • Selector.

Let's take a look at the simple figure:

Basically, there is a dispatcher missing from the Reactor. This is because the nio provided by jdk itself is quite basic, and dispatcher is generally implemented by ourselves. In my understanding, the mina and netty frameworks provide implementation of this framework.

V. Example

I copied an example and diagram from the netty authoritative guide, and the Code does not have a client. You can take a look at it (why not? Because it's a little faster, I don't want to write ......):
Server sequence diagram:

Client sequence diagram:

Server code:

Package com. gj. netty. nio; import java. io. IOException; import java.net. inetSocketAddress; import java. nio. channels. selectionKey; import java. nio. channels. selector; import java. nio. channels. serverSocketChannel; import java. util. iterator; import java. util. set;/*** Created by guojing on 2015/6/7. */public class MultiplexerTimerServer implements Runnable {private Selector selector; private ServerSocketChanne L servChannel; private volatile boolean stop; public MultiplexerTimerServer (int port) {try {selector = Selector. open (); // create a multiplexing selector servChannel = ServerSocketChannel. open (); // create a channel servChannel. configureBlocking (false); // set non-blocking servChannel. socket (). bind (new InetSocketAddress (port), 1024); // port, block size servChannel. register (selector, SelectionKey. OP_ACCEPT); System. out. println ("TimeServer is s Tart, port: "+ port);} catch (IOException e) {e. printStackTrace () ;}} public void run () {while (! Stop) {try {selector. select (1000); Set <SelectionKey> keys = selector. selectedKeys (); Iterator <SelectionKey> ketIt = keys. iterator (); SelectionKey key = null; while (ketIt. hasNext () {key = ketIt. next (); ketIt. remove (); // handle the corresponding key event handler (key) ;}} catch (IOException e) {e. printStackTrace () ;}} private void handler (SelectionKey key) {// remove the channel according to the key for corresponding processing }}View Code 6. Last point

I think some people will scold me for the title if they remember it now. It will take at least half an hour to read it carefully. I can only say that if you have understood it before, it doesn't matter if you give a 10-minute glance. If you haven't understood it before, if this article gives you a better understanding, it doesn't matter how much time it takes. Knowing java nio is a matter of accumulation, and understanding the ideas and principles behind it is a matter of accumulation. Besides, I plan to finish writing in half an hour. It will have been more than two hours ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.