Processing of NIO by the JVM Library

Source: Internet
Author: User

IO selection of the JVM

Check the JVM source code, just see a piece of the JVM library:

     Public StaticSelectorproviderCreate() {String osname = accesscontroller.doprivileged (NewGetpropertyaction ("Os.name"));if("SunOS". Equals (Osname)) {return NewSun.nio.ch.DevPollSelectorProvider (); }//Use Epollselectorprovider for Linux kernels >= 2.6        if("Linux". Equals (Osname)) {String osversion = accesscontroller.doprivileged (NewGetpropertyaction ("Os.version")); string[] vers = Osversion.split ("\\.",0);if(Vers.length >=2) {Try{intMajor = Integer.parseint (vers[0]);intMinor = Integer.parseint (vers[1]);if(Major >2|| (Major = =2&& Minor >=6)) {return NewSun.nio.ch.EPollSelectorProvider (); }                }Catch(NumberFormatException x) {//Format not recognized}            }        }return NewSun.nio.ch.PollSelectorProvider (); }

If the 2.6 version after the use of epoll, it is basically no need to deliberately use AIO.

Talk about IO blocking IO

It is a classic mode of communication in which both read and write operations are blocked while the processing thread is not available for execution of other tasks during the blocking process. From a machine to B machine its communication process is: a machine a thread to the socket to write data, after writing to wait for the data, B machine a thread to the socket read data to a machine to write data, and then wait for a machine next transfer data, and constantly cycle this interaction until the completion of communication. This process can see that both the A machine and the B machine are responsible for reading and writing the thread to enter the waiting state, this way is blocking the IO mode.

Non-blocking IO

Blocking makes the machine less efficient because any connection read or write can suspend the CPU. Therefore, a non-blocking IO is provided, and the execution thread first determines whether a connection is readable or writable, and only a readable or writable connection is performed to perform a specific read-write operation, so that the thread of execution is not suspended from the CPU, and the threads are always traversing all connections and the machine is utilized.

Event-driven non-blocking IO

However direct-to-connection traversal is CPU-intensive, and when the number of connections is large, traversing is also a heavy operation. So the operating system kernel continues to improve, to provide an event-driven non-blocking IO, the core idea is that the application process will be concerned about the list of events to tell the kernel, the system kernel will be a readable writable connection to update the application process to follow the list of events, the application Process traversal event list to know which connections are available, The kernel is responsible for maintaining the list of events, and the application process gets the list of events to perform detailed read and write operations on the corresponding connection.

Asynchronous IO

Asynchronous IO is more like another style, which is that the kernel calls the callback function corresponding to the connection whenever a connection is readable and writable, rather than updating the event list.

Synchronization and non-synchronization are another dimension, and no discussion is introduced here.

Processing of NIO by the JVM Library

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.