API笔记之java.nio.channels.Selector

API笔记之java.nio.channels.Selector

Java代码 
  1. import java.io.Closeable;  
  2. import java.io.IOException;  
  3. import java.nio.channels.spi.SelectorProvider;  
  4. import java.util.Set;  
  5.   
  6.   
  7. /** 
  8.  *  
  9.  * A multiplexor of {@link SelectableChannel} objects. 
  10.  * <pre> 
  11.  *                      Selector 
  12.  *                         ^ 
  13.  * ________________________|________________________________ 
  14.  *    |          |                     |               | 
  15.  *   ...    SelectionKey          SelectionKey        ... 
  16.  *               |                     | 
  17.  *   ...   SelectableChannel     SelectableChannel    ... 
  18.  *  
  19.  *  
  20.  * 以下两种创建方式: 
  21.  * 1.调用Selector.open() 
  22.  * 2.调用java.nio.channels.spi.SelectorProvider.openSelector() 
  23.  *  
  24.  * 有三种key set: 
  25.  * 1.registered key set. 已注册的键集 
  26.  * 2.selected key set, 已选择的键集, 之中每一个key的channel都至少准备好一种操作  
  27.  * 3.cancelled key set, 已取消的键集, 调用SelectionKey.cancel()后, 那个SelectionKey 
  28.  * 就被加到这里, 在下一次select()/selectNow()/select(long)时, 该SelectionKey就会被移除. 
  29.  * <p> 
  30.  * select()/selectNow()/select(long)的过程: 
  31.  * 1.cancelled key set中的每个key都会从另两个key set中移除, 对应的通道也被注销, cancelled key set被清空。 
  32.  * 2.registered key set 中的key的inters集合将被检查, 在这个步骤中的检查执行完后, 对interst集合更改不会影响接下来的过程, 
  33.  *   接下来底层操作系统将进行查询, 以确定每个通道所关心的操作的真是就绪状态.系统调用完成后, 对那些还没准备好的通道将不会执行任何操作,  
  34.  *   对那些已准备好的通道, 将执行以下两种操作之一: 
  35.  *    
  36.  *   a.如果通道的键还没有处于已选择的键的集合, 那么键的ready集合将被清空, 然后表示操作系统发行的但钱通道已经准备好的操作的比特掩码将被设置. 
  37.  *    
  38.  *   b.否则, 也就是在已选择的键的集合中, 键的ready集合将被表示操作系统发现的当前已经准备好的操作的比特掩码更新。所有之前的已经不再是就绪状 
  39.  *   态的操作不会被清除.事实上, 所有的比特位都不会被清理,由操作系统决定的ready集合是与之前的ready集合按位分离的, 一旦键被放置于选择器的 
  40.  *   已选择的键的集合中, 他的ready集合将是累积的,比特位只会被设置, 不会被清理. 
  41.  * 3.步骤2发生过程中, 有可能有些键被取消了, 因此在步骤2执行完后步骤1将再次执行, 以确保键已经被取消的通道的注销 
  42.  * 4.select操作返回的值是ready集合在步骤2中被修改的键的数量, 而不是已选择的键的集合中的通道的数量. 返回值不是已准备好的通道的总数, 而是 
  43.  *   是从上一个select()调用之后进入就绪状态的通道的数量。之前的调用中就绪的, 并且在本次调用中依然就绪的不会被计入, 而那些在前一次调用中已经 
  44.  *   就绪但现在已经不处于就绪的通道也不会被计入,这些通道可能仍然在已选择的键的集合中, 但不会被计入返回值. 
  45.  *</p>  
  46.  *<p> 
  47.  * 有三个选择的方法select()、select(long)和selectNow(), 前两个会阻塞当前线程, 而在这阻塞过程中, 有三种方法可以使线程退出阻塞状态: 
  48.  * 1.Selector.wakeup()方法, 使选择器上的第一个还没有返回的选择操作立即返回,如果当前没有进行中的选择操作, 那下一次select()方法的一种 
  49.  *   形式的调用将立即返回. 后面的选择操作正常执行 
  50.  * 2.Selector.close()方法. 任何一个在选择操作中阻塞的线程都将被唤醒, 与选择器相关的通道将被注销, 而键被取消 
  51.  * 3.Thread.currentThread.interrupt(), 该阻塞是可以响应中断的. 
  52.  *  
  53.  * </p> 
  54.  * </pre> 
  55.  */  
  56.   
  57. public abstract class Selector implements Closeable {  
  58.   
  59.     protected Selector() { }  
  60.   
  61.     /** 
  62.      * Opens a selector. 
  63.      */  
  64.     public static Selector open() throws IOException {  
  65.         return SelectorProvider.provider().openSelector();  
  66.     }  
  67.   
  68.     /** 
  69.      * Tells whether or not this selector is open. 
  70.      */  
  71.     public abstract boolean isOpen();  
  72.   
  73.     /** 
  74.      * Returns the provider that created this channel. 
  75.      */  
  76.     public abstract SelectorProvider provider();  
  77.   
  78.     /** 
  79.      * Returns this selector's registered-key set. 
  80.      */  
  81.     public abstract Set<SelectionKey> keys();  
  82.   
  83.     /** 
  84.      * Returns this selector's selected-key set. 
  85.      */  
  86.     public abstract Set<SelectionKey> selectedKeys();  
  87.   
  88.     /** 
  89.      * Selects a set of keys whose corresponding channels are ready for I/O 
  90.      * operations.不会阻塞 
  91.      */  
  92.     public abstract int selectNow() throws IOException;  
  93.   
  94.     /** 
  95.      * Selects a set of keys whose corresponding channels are ready for I/O 
  96.      * operations.可能阻塞, 但最多阻塞 timeout ms 
  97.      */  
  98.     public abstract int select(long timeout)  
  99.         throws IOException;  
  100.   
  101.     /** 
  102.      * Selects a set of keys whose corresponding channels are ready for I/O 
  103.      * operations.可能阻塞, 直到底层系统调用完成 
  104.      */  
  105.     public abstract int select() throws IOException;  
  106.   
  107.     /** 
  108.      * Causes the first selection operation that has not yet returned to return 
  109.      * immediately. 
  110.      */  
  111.     public abstract Selector wakeup();  
  112.   
  113.     /** 
  114.      * Closes this selector. 
  115.      */  
  116.     public abstract void close() throws IOException;  
  117.   
  118. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值