Does JDK7 use NIO.2 Epoll etc. In Linux?

I am studying the architecture of network I / O interfaces in different languages ​​and have a query about how Async IO APIs are implemented in Java. "Old" streaming IO APIs (up to 1.4) offer synchronous locking of read / write functions. The JDK 1.4 NIO APIs use epoll / select to test IO readiness (for users through SelectableChannel and Selector, etc.). This is probably both Windows and * nix. This is a sample reactor. Now JDK7 introduced the NIO.2 API, which, among other things, offers an asynchronous API (proactor template) and uses internal (custom) thread pools to enter IO in the background and call back the user code upon completion. It seems to be using IOCP for Windows, but I was wondering: 1. what it uses on Linux, which is my main platform of interest. Does it use epoll and friends or uses threadpool to block I / O? 2. Is the actual I / O value in NIO.2 (independent of the platform) executed by user threads in a Java thread, or executed by kernel threads, and are Java thread thread threads responsible for simply copying byte buffers and returning user code?

+6
source share
1 answer

If linux kernel> = 2.6 is found, then java.nio.channels.spi.SelectorProvider will use epoll.

Here is a fragment of the DefaultSelectorProvider.java source (from Java 7):

 public static SelectorProvider create() { String osname = AccessController.doPrivileged( new GetPropertyAction("os.name")); if ("SunOS".equals(osname)) { return new sun.nio.ch.DevPollSelectorProvider(); } // use EPollSelectorProvider for Linux kernels >= 2.6 if ("Linux".equals(osname)) { String osversion = AccessController.doPrivileged( new GetPropertyAction("os.version")); 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(); } 

Both NIO 2 and "original" (let it be called NIO 1) should use low-level event notification mechanisms or the Linux AIO API (which is relatively new), because you never know what the kernel of your application will be on the machine starts up. I would not be surprised to see that NIO 2 actually uses Linux AIO or POSIX AIO (they are completely different).

+5
source

All Articles