Java NIO,
Java NIO,
Why NIO ?
http://onjava.com/pub/a/onjava/2002/09/04/nio.html?page=1
Java developers might ask: why introducing a new technology to handle sockets? What's wrong with the Java 1.3.x sockets? Suppose you would like to implement a server accepting diverse client connections. Suppose, as well, that you would like the server to be able to process multiple requests simultaneously. Using Java 1.3.x, you have two choices to develop such a server:
- Implement a multithread server that manually handles a thread for each connection.
- Using an external third-party module.
Both solutions work, but adopting the first one -- the whole thread-management solution, with related concurrency and conflict troubles -- has to be developed by programmer. The second solution may cost money, and it makes the application dependent on a non-JDK external module. By means of the nonblocking socket, you can implement a nonblocking server without directly managing threads or resorting to external modules.
In internal benchmarks, the router was able to handle up to 10,000 clients with no significant drop in throughput. For comparison, we implemented a version based on the thread-per-client model, which was only able to reach 3,000 clients, with a significant drop in throughput as the number of clients increased. (Thread Pool not alleviate it)
http://java.sun.com/j2se/1.4.2/docs/guide/nio/example/index.html
http://rox-xmlrpc.sourceforge.net/niotut/
Q: use loop to read or read assembling ?
For the loop read:
1. any possible dead loop ?
2. If socket channel is in non-blocking mode, cpu is busy waiting set non blocking mode
3. If in blocking, then this operation become blocked
Q: need addInterestOps each time after the interest operation is done ?
Q: A selector, its selection keys, and registered channels should never be accessed by more than one thread.
If a selection key is changed in any way by a thread while another thread is calling its selector select()
method, the typical result is the select()
call aborting with an ugly exception.
相关文章
- 暂无相关文章
用户点评