Java NIO

本文探讨了Java NIO技术的优势,特别是在处理大量并发连接时的表现。通过对比传统的多线程服务器模型,展示了NIO在提高吞吐量和减少资源消耗方面的显著效果。此外,还讨论了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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值