java NIO --selector
2021-06-23 18:04
标签:uri 自定义 操作 bin ted sed pre str create 传统的IO 编程中,针对每一个客户端连接都会创建一个新的线程; 而 NIO 一个线程可以处理很多客户端的请求 我们分析源码的注解,可以归纳出: 1. Selector 构造方式:(常见的Seletor 构造方式) 源码: * A selector may be created by invoking the {@link #open open} method of 翻译: Selector 是通过调用这个类的open()方法进行创建的,它使用的是系统默认的 选择器提供者去创建一个新的Selector; 也可以调用spi 里的 openSelector 方法进行自定义Selector 的创建; 在调用close 方法之前,Selector 是保持打开的; 结果: 2. 关于通道的注册以及selector 管理通道发生的事件(selector 是通过事件进行监听的) 翻译解析: 我们可以通提供过继承了 //创建Selector 选择器对象 //创建ServerSocketChannel ServerSocketChannel socketChannel = ServerSocketChannel.open(); //进行通道的注册 socketChannel.register(selector, SelectionKey.OP_ACCEPT); 3.. SelectionKey : SelectionKey 是 java NIO 中重要的值(我们可以认为是通道注册的唯一值): 翻译: 一个选择器里维护了三种选择集 分别是如下: * The key set contains the keys representing the current The selected-key set is the set of keys such that each The cancelled-key set is the set of keys that have been 1. 键集:包含了选择器里当前通道注册的键,可以通过keys 方法进行返回这个键集;它是最全所有的键集 3. cancelled 键集:是一个被取消的键集,但是通道还没有被撤销登记,这几集合不能直接被操作,也是键集(1)的子类 java NIO --selector 标签:uri 自定义 操作 bin ted sed pre str create 原文地址:https://www.cnblogs.com/iscys/p/9672318.html
* this class, which will use the system‘s default {@link
* java.nio.channels.spi.SelectorProvider selector provider} to
* create a new selector. A selector may also be created by invoking the
* {@link java.nio.channels.spi.SelectorProvider#openSelector openSelector}
* method of a custom selector provider. A selector remains open until it is
* closed via its {@link #close close} method.Selector selector = Selector.open(); //Selector 构造方式
源码:
* p> A key is added to a selector‘s key set as a side effect of registering a
* channel via the channel‘s {@link SelectableChannel#register(Selector,int)
* register} method. Cancelled keys are removed from the key set during
* selection operations. The key set itself is not directly modifiable. 需要注意的是 只有继承了 SelectableChannel 的类 才能够进行注册;
Selector selector = Selector.open();
//设置非阻塞模式
socketChannel.configureBlocking(false);
//创建ServerSocket 绑定端口
ServerSocket socket=socketChannel.socket();
socket.bind(new InetSocketAddress(9999));源码:
* p> A selectable channel‘s registration with a selector is represented by a
* {@link SelectionKey} object. A selector maintains three sets of selection
* keys:
selector 选择器中可选择的通道注册是由 SelectionKey 是可以追溯的,也可以通过 SelectionKey 来获取当前 SelectionKey 的通道channel;
源码:
* channel registrations of this selector. This set is returned by the
* {@link #keys() keys} method.
*
*
* key‘s channel was detected to be ready for at least one of the operations
* identified in the key‘s interest set during a prior selection operation.
* This set is returned by the {@link #selectedKeys() selectedKeys} method.
* The selected-key set is always a subset of the key set.
*
*
* cancelled but whose channels have not yet been deregistered. This set is
* not directly accessible. The cancelled-key set is always a subset of the
* key set. 翻译理解:
下一篇:数据结构与算法笔试面试题整理