API笔记之java.nio.channels.SelectionKey

API笔记之java.nio.channels.SelectionKey

 

Java代码 
  1. import java.nio.channels.SelectableChannel;  
  2. import java.nio.channels.Selector;  
  3. import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;  
  4.   
  5. /** 
  6.  * A token representing the registration of a {@link SelectableChannel} with a 
  7.  * {@link Selector}. 
  8.  *  
  9.  * A token representing the registration of a SelectableChannel with a Selector.  
  10.  * 一个SelectoableChannel在Selector中注册之后就产生一个SelectionKey对象,表示我们对这个SelectoableChannel的什么事件感兴趣,当然 
  11.  * SelectionKey的interestOps是我们自己传进去的, 我们调用selector的selectedKey方法会返回被选中的SelectionKey集,通过这个key判断 
  12.  * 什么事件,然后通过channel()方法进行io操作 
  13.  */  
  14.   
  15. public abstract class SelectionKey {  
  16.     /** 
  17.      * 四种事件用一个int(intrestOps)变量表示(其实byte也够了?) 
  18.      *  bits: [0|1]    [0|1]   [0|1]    [0|1]  
  19.      * means: accept  connect  write    read 
  20.      * 想要哪几种事件,就对这些事件的值进行或运算,得出的就是intrestOps 
  21.      *比如 interestOps = 2(0010),该key会被选中, 事件是write, 主线逻辑拿得到这个key对应的channel可以进行写操作 
  22.      */  
  23.     /** 
  24.      * 当selector发现:  
  25.      * 1.channel有数据可读 
  26.      * 2.channel has been remotely shut down for further reading(不能100%确定其意思,猜测是channel的读通道关闭) 
  27.      * 3.channel has an error pending,有错误待处理? 
  28.      * 时, selector就会把该key加到selected key set中 
  29.      */  
  30.     public static final int OP_READ = 1 << 0;  
  31.   
  32.     /** 
  33.      *这三个和上面差不多 
  34.      */  
  35.     public static final int OP_WRITE = 1 << 2;  
  36.     public static final int OP_CONNECT = 1 << 3// 这个只有SocketChannel才有  
  37.     public static final int OP_ACCEPT = 1 << 4// 这个只有ServerSocketChannel才有  
  38.   
  39.     /** 
  40.      * 不给外部new实例, 在SelectionKeyImpl中会开放这个构造方法给包内的lei使用 
  41.      */  
  42.     protected SelectionKey() { }  
  43.   
  44.     /** 
  45.      * Returns the channel for which this key was created.  This method will 
  46.      * continue to return the channel even after the key is cancelled. 
  47.      */  
  48.     public abstract SelectableChannel channel();  
  49.   
  50.     /** 
  51.      * Returns the selector for which this key was created.  This method will 
  52.      * continue to return the selector even after the key is cancelled. 
  53.      */  
  54.     public abstract Selector selector();  
  55.   
  56.     /** 
  57.      * Tells whether or not this key is valid. 
  58.      * 
  59.      * 当key被创建开始就是valid的,直到以下情况发生: 
  60.      * 1.cancel()方法被调用 
  61.      * 2.它对应的channel的被close 
  62.      * 3.它对应的selector被close 
  63.      */  
  64.     public abstract boolean isValid();  
  65.   
  66.     /** 
  67.      * Requests that the registration of this key's channel with its selector 
  68.      * be cancelled.   
  69.      * 被调用后,该key就是invalid的了,会被加到对应的selector的cancelled key set中,当selector的下一次选择操作时,  
  70.      * 该key就会被移除(selector的三种key set) 
  71.      */  
  72.     public abstract void cancel();  
  73.   
  74.     /** 
  75.      * Retrieves this key's interest set. 
  76.      */  
  77.     public abstract int interestOps();  
  78.   
  79.     /** 
  80.      * Sets this key's interest set to the given value. 
  81.      */  
  82.     public abstract SelectionKey interestOps(int ops);  
  83.   
  84.     /** 
  85.      * Retrieves this key's ready-operation set. 
  86.      */  
  87.     public abstract int readyOps();  
  88.   
  89.     /** 
  90.      * Tests whether this key's channel is ready for reading. 
  91.      */  
  92.     public final boolean isReadable() {  
  93.         return (readyOps() & OP_READ) != 0;  
  94.     }  
  95.   
  96.     /** 
  97.      * Tests whether this key's channel is ready for writing. 
  98.      */  
  99.     public final boolean isWritable() {  
  100.         return (readyOps() & OP_WRITE) != 0;  
  101.     }  
  102.   
  103.     /** 
  104.      * Tests whether this key's channel has either finished, or failed to 
  105.      * finish, its socket-connection operation. 
  106.      */  
  107.     public final boolean isConnectable() {  
  108.         return (readyOps() & OP_CONNECT) != 0;  
  109.     }  
  110.   
  111.     /** 
  112.      * Tests whether this key's channel is ready to accept a new socket 
  113.      * connection. 
  114.      */  
  115.     public final boolean isAcceptable() {  
  116.         return (readyOps() & OP_ACCEPT) != 0;  
  117.     }  
  118.     /** 
  119.      * 绑定的对象 
  120.      */  
  121.     private volatile Object attachment = null;  
  122.   
  123.     //原子性更新引用类型的变量  
  124.     private static final AtomicReferenceFieldUpdater<SelectionKey,Object>  
  125.         attachmentUpdater = AtomicReferenceFieldUpdater.newUpdater(  
  126.             SelectionKey.class, Object.class"attachment"  
  127.         );  
  128.   
  129.     /** 
  130.      * Attaches the given object to this key. 
  131.      */  
  132.     public final Object attach(Object ob) {  
  133.         return attachmentUpdater.getAndSet(this, ob);  
  134.     }  
  135.   
  136.     /** 
  137.      * Retrieves the current attachment. 
  138.      */  
  139.     public final Object attachment() {  
  140.         return attachment;  
  141.     }  
  142.   
  143. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值