====笔记=====
Java NIO提供选择器(Selectors),选择器提供选择执行已经就绪的任务的能力,使得多元IO成为可能。就绪选择和多元IO使得单线程能够有效率地同时管理多个I/O通道(channels)。
——一种新的IO模型
我的理解:一个工作线程通过“选择器”监控多个IO通道,当IO通道没有就绪的任务时,该线程可以做其他事情(不用阻塞),当IO通道上有任务就绪时,该线程能够处理该任务。
三个主要的类
keyword: 可选择通道 注册 选择器对象 select() 选择键 选择键集合 就绪的通道
疑问:将通道(Channel)注册(register)到选择器(Selector),注册方法是通道提供还是选择器提供呢?
——观察者模式 http://nemogu.iteye.com/blog/1407857
SelectionKey register(Selector sel, int ops, Object att)
Registers this channel with the given selector, returning a selection key.
在visitor模式中,访问者访问节点,首先通过node.accept(visitor)
一点收获:如果将A注册到B,则A提供一个register方法,a.register(b)
Registers this channel with the given selector, returning a selection key.
在visitor模式中,访问者访问节点,首先通过node.accept(visitor)
一点收获:如果将A注册到B,则A提供一个register方法,a.register(b)
观察者模式中也是由主题来提供register方法,主题调用该注册方法将观察者注册进来。
SelectionKey(选择键)
一个selectableChannel可以注册到多个selector上,并且一个selector也可以监控多个selectableChannel,通过选择键来表示一个通道和选择器之间的注册关系。