Deep understanding of Java NIO

Source: Internet
Author: User
Tags epoll

Initial knowledge of NiO:

In JDK 1. The new Input/output class, introduced in 4, introduces a channel-and buffer-based I/O approach that can allocate out-of-heap memory directly using the Native library, and then through a Directbytebuffer object stored in the Java heap Operation as a reference to this memory avoids copying data back and forth in the Java heap and the Native heap.

NIO is a synchronous, non-blocking IO model. Synchronization refers to whether threads are constantly polling for IO events in readiness, and non-blocking means that threads can do other tasks at the same time while waiting for IO. The core of synchronization is the selector,selector instead of the thread itself polling IO events, to avoid blocking while reducing unnecessary thread consumption, the core of non-blocking is the channel and buffer, when the IO event is ready, you can write the buffer to ensure the success of the IO, without the need to wait for the thread blocking.

Buffer:

Why is NiO a buffer-based IO method? Because when a link is established, the IO data may not arrive immediately, in order to complete the IO operation correctly when the data arrives, in the bio (blocking IO), the thread waiting for IO must be blocked to perform IO operations 24x7. In order to solve the problem of inefficient IO mode, the concept of buffer is introduced, when the data arrives, it can be written to the buffer in advance, then the buffer is handed to the thread, so the thread does not need to wait for IO to block.

Channel:

When executed: socketchannel.write (buffer), a buffer is written to a channel. If the buffer is well understood, the channel is relatively more abstract. Online blog inevitably have to write not rigorous place, easy to make beginners feel difficult to understand.

Referring to the authority in Java NIO: A channel is the entry through which I/O transmissions occur, and buffers are the source or destination of these data transmissions. For a transfer away from the buffer, the data you want to pass out is placed in a buffer and transmitted to the channel. For the transfer back buffer, a channel places the data in the buffer you provide.

For example, there is a server channel Serversocketchannel Serverchannel, a client channel Socketchannel clientchannel; server buffers: Serverbuffer, Client buffer: Clientbuffer.

When the server wants to send data to the client, it needs to call: Clientchannel.write (Serverbuffer). Call Clientchannel.read (Clientbuffer) When the client is about to read

When the client wants to send data to the server, it needs to call: Serverchannel.write (Clientbuffer). When the server is going to read, call Serverchannel.read (Serverbuffer)

In this way, the channel and buffer relationships seem to be better understood. In practice, it is not necessarily the folly of this two-way connection (which does exist, however, which will be covered later), but it can be understood that in NiO: if you want to send data to the target, you need to write the buffer that stores the data to the channel in the target side. The data is then read from the channel to buffer on the target side.

Selector:

the channel and buffer mechanisms allow threads to wait for IO events to be ready without blocking, but there is always someone to monitor these IO events. This work is given to the selector to complete, which is called synchronization.

Selector allows single-threaded processing of multiple Channel. If your app has multiple connections (channels) open, but the traffic is low for each connection, it's convenient to use selector.

To use selector, you have to register the channel with selector and then call its select () method. This method will always block to a registered channel with event readiness, which is called polling. Once this method returns, the thread can handle these events.

The events of interest registered in Selector are:

    • Op_accept

    • Op_connect

    • Op_read

    • Op_write

Optimization:

An optimization method is to further decompose selector into reactor, separating different events of interest, and each reactor is responsible for only one event of interest. The advantages of this are: 1. Isolate the blocking level and reduce the polling time; 2. Threads do not have to traverse set to find events of interest to them, because the resulting set contains only events of interest to them.

NiO and Epoll:

Epoll is the IO model of the Linux kernel. I think there must be someone who wants to ask if aio sounds bigger than nio, why not use AIO? AIO actually has applications, but one problem is that Linux does not support AIO, so the AIO-based program runs on Linux less efficiently than NIO. While Linux is the most important server OS, NIO is now more widely used than AIO.

Speaking of which, you may already understand that Epoll must have a deep cause with NIO. Yes, if you look closely at Epoll's technical insider, you will find that it is really similar to NIO, based on "channel" and buffer, and selector, but in Epoll, the channel is actually the "pipe" of the operating system. Unlike NiO, in NiO, the thread is freed, but it needs to be selector blocked to poll IO events ready, and in Epoll, when the IO event is ready, the message is sent automatically, notifying selector: "I'm ready." "It can be argued that the Linux epoll is a more efficient nio.

NiO Anecdotes:

An interesting blog, speaking of Java Selector.open (), will create a link between yourself and yourself (on Windows is tcp,linux on the channel)

The reason for doing this: You can spy from Apache Mina. In Mina, there are the following mechanisms:

    1. The Mina framework creates a thread for a work object.

    2. The run () method of the work object's thread takes a bunch of channel from a queue and then uses the Selector.select () method to listen for data that can be read/written.

    3. Most crucially, at select time, if the queue has a new channel join, then Selector.select () will be awakened and then re-select the latest channel collection.

    4. To wake up the Select method, you only need to call Selector's wakeup () method.

A thread that is blocked on select can be awakened in the following three ways:

    1. There is data to read/write, or an exception occurs.

    2. Blocking time, that is, when out.

    3. Receive a non-block signal. May be issued by kill or Pthread_kill.

First 2 can be excluded, and the third Way, only exists in Linux. So why is Java NiO creating a link to itself and itself: if you want to wake up Select, you just need to connect the data to your own loopback, so you can wake up the threads that are blocking on the select.

Deep understanding of Java NIO

Related Article

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.