Internal implementation of the Java NIO selector (Selector) (poll epoll)

Source: Internet
Author: User
Tags epoll

http://blog.csdn.net/hsuxu/article/details/9876983

Before emphasizing so much about the Linux kernel poll and epoll, just want to let everyone first have a knowledge:


The selectors in Java NiO rely on these system calls from the operating system kernel, and we'll only talk about the NIO implementations associated with the Linux kernel, and, of course, windows or other operating system implementations are broadly similar, and I believe you can comprehend by analogy.


Then, this article from here will be simple to difficult, step by step for you to explain the selector of the point drop it.


Macro understanding of selectors
"There is such an inspector, she works on a chicken farm, the daily job is to constantly look at a particular chicken coop, if there is chicken eggs, or need to feed, or a chicken is sick, the corresponding information is recorded, so that the chicken coop owner wants to know the situation of the chicken coop, only need to go to the inspectors to inquire, of course, The owner of the chicken coop must inform the inspector in advance which chicken coop to inquire. “


The above paragraph is a metaphor for the work of the selector, in fact the selector for the channel service, the channel in advance to tell the selector: "I am interested in certain events, such as readable, writable, etc.", the selector in the acceptance of one or more channels of the Commission, the choice of work, its choice of work is completely given to the operating system, Linux is the poll or Epoll.


Creation of selectors
When Selector.open () is called, the selector creates a selector implementation with a dedicated factory Selectorprovider, Selectorprovider masking the differences between different operating systems and version creation implementations. The specific implementation code is as follows:


Java.nio.channels.Selector




public static Selector open () throws IOException {
Return Selectorprovider.provider (). Openselector ();
}
Because Selectorprovider itself is an abstract class, it provides a corresponding provider implementation by calling provider (), such as Pollselectorprovider, Epollselectorprovider


Java.nio.channels.spi.SelectorProvider


public static Selectorprovider provider () {
Synchronized (lock) {
if (provider! = NULL)
return provider;
Return (Selectorprovider) Accesscontroller
. doprivileged (New Privilegedaction () {
Public Object Run () {
if (Loadproviderfromproperty ())
return provider;
if (Loadproviderasservice ())
return provider;
Provider = Sun.nio.ch.DefaultSelectorProvider.create ();
return provider;
}
});
}
}
The default provider implementation is Defaultselectorprovider, by calling Create (), to obtain a specific selectorprovider


Sun.nio.ch.DefaultSelectorProvider




public static Selectorprovider Create () {
Privilegedaction pa = new Getpropertyaction ("Os.name");
String osname = (string) accesscontroller.doprivileged (PA);
if ("SunOS". Equals (Osname)) {
return new Sun.nio.ch.DevPollSelectorProvider ();
}

Use Epollselectorprovider for Linux kernels >= 2.6
if ("Linux". Equals (Osname)) {
PA = new Getpropertyaction ("Os.version");
String osversion = (string) accesscontroller.doprivileged (PA);
string[] vers = osversion.split ("\ \", 0);
if (vers.length >= 2) {
try {
int major = Integer.parseint (Vers[0]);
int minor = Integer.parseint (Vers[1]);
if (Major > 2 | | (Major = = 2 && minor >= 6)) {
return new Sun.nio.ch.EPollSelectorProvider ();
}
} catch (NumberFormatException x) {
Format not recognized
}
}
}

return new Sun.nio.ch.PollSelectorProvider ();
}
This is the implementation of the Linux operating system Defaultselectorprovider, you can see, if the kernel version >=2.6, the specific selectorprovider is Epollselectorprovider, Otherwise, the default Pollselectorprovider


By combining the above, you can guess that the selector provided by Epollselectorprovider is definitely related to the kernel Epoll, Pollselectorprovider provides
Selector must have something to do with poll. This is true:


Sun.nio.ch.EPollSelectorProvider




Public Abstractselector Openselector () throws IOException {
return new Epollselectorimpl (this);
}
Sun.nio.ch.PollSelectorProvider


Public Abstractselector Openselector () throws IOException {
return new Pollselectorimpl (this);
}

Internal implementation of the Java NIO selector (Selector) (poll epoll)

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.