Calvin's Tech Space

成于坚忍,毁于浮躁

   :: 首页 :: 联系 :: 聚合  :: 管理

参见
The Rox Java NIO Tutorial

一些principles
1.Use a single selecting thread
虽然selector本身是线程安全的,但是key集合却不是线程安全的,使用多线程容易导致死锁。

2.Use a single selecting thread
NIO底层实现在不同的平台上是不一样的,如果你编写的程序可能要在不同的平台上运行,那么必须遵循这一原则。
更改行为包括:修改一个select key感兴趣的ops,向这个selector注册新的channel或者从这个selector取消一个channel等。

3.Set OP_WRITE only when you have data ready
A common mistake is to enable OP_WRITE on a selection key and leave it set. This results in the selecting thread spinning because 99% of the time a socket channel is ready for writing. In fact the only times it's not going to be ready for writing is during connection establishment or if the local OS socket buffer is full. The correct way to do this is to enable OP_WRITE only when you have data ready to be written on that socket channel. And don't forget to do it from within the selecting thread.

4.Alternate between OP_READ and OP_WRITE



posted on 2009-12-27 13:55 calvin 阅读(389) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: