Java nio implementation in windows

Source: Internet
Author: User
Java 1.4 provides nio, which is the multiplexed non-blocking I/O mentioned in my previous blog. This model has better concurrency performance than the blocking model. Many network applications in Java rewrite the underlying modules, including Tomcat and Jetty, the nio-based framework mina and cindy Made in China also appeared.

The impact of java nio is huge and has gained a lot of support and appreciation.

However, there are some rumors that the implementation in windows is the I/O model IOCP with the best concurrency performance in Windows, but is it true?

JDK 6.0 RC version provides source code download, download path: http://www.java.net/download/jdk6/jdk-6-rc-src-b104-jrl-01_nov_2006.jar

We can see the final implementation of Windows:
J2se \ src \ windows \ native \ sun \ nio \ ch \ WindowsSelectorImpl. c

Start from line 82:/** // * Call select */
If (result = select (0, & readfds, & writefds, & effectfds, TV ))
= SOCKET_ERROR ){
/** // * Bad error-this shoshould not happen frequently */
/** // * Iterate over sockets and call select () on each separately */
FD_SET errreadfds, errwritefds, and errexceptfds;
Readfds. fd_count = 0;
Writefds. fd_count = 0;
Required TFDs. fd_count = 0;
For (I = 0; I <numfds; I ++ ){
/** // * Prepare select structures for the I-th socket */
Errreadfds. fd_count = 0;
Errwritefds. fd_count = 0;
If (fds [I]. events & POLLIN ){
Errreadfds. fd_array [0] = fds [I]. fd;
Errreadfds. fd_count = 1;
}
If (fds [I]. events & (POLLOUT | POLLCONN )){
Errwritefds. fd_array [0] = fds [I]. fd;
Errwritefds. fd_count = 1;
}
Errexceptfds. fd_array [0] = fds [I]. fd;
Errexceptfds. fd_count = 1;

/** // * Call select on the I-th socket */
If (select (0, & errreadfds, & errwritefds, & errexceptfds, & zerotime)
= SOCKET_ERROR ){
/** // * This socket causes an error. Add it to your TFDs set */
Required TFDs. fd_array [limit TFDs. fd_count] = fds [I]. fd;
Using TFDs. fd_count ++;
} Else {
/** // * This socket does not cause an error. Process result */
If (errreadfds. fd_count = 1 ){
Readfds. fd_array [readfds. fd_count] = fds [I]. fd;
Readfds. fd_count ++;
}
If (errwritefds. fd_count = 1 ){
Writefds. fd_array [writefds. fd_count] = fds [I]. fd;
Writefds. fd_count ++;
}
If (errexceptfds. fd_count = 1 ){
Required TFDs. fd_array [limit TFDs. fd_count] = fds [I]. fd;
Using TFDs. fd_count ++;
}
}
}
}

This is the select model widely used in Winsock. It is well known that the concurrency performance is not very good. In addition, FD_SETSIZE cannot exceed the provider limit for lower Windows, which is usually 1024. That is to say, in Windows, the JDK nio model cannot have more than 1024 connections, which is similar to the test result I have previously tested.

In addition, if FD_SETSIZE is large, for example, 1000, 1000 sockets must be set before the select statement is called, and the 1000 sockets must be checked after the result is returned.

In other words, using sun jdk java nio in Windows does not improve the concurrency performance.

Wenshao log comment

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.