- 浏览: 985022 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
Channel接口定义:http://donald-draper.iteye.com/blog/2369111
AbstractInterruptibleChannel接口定义:http://donald-draper.iteye.com/blog/2369238
SelectableChannel接口定义:http://donald-draper.iteye.com/blog/2369317
SelectionKey定义:http://donald-draper.iteye.com/blog/2369499
SelectorProvider定义:http://donald-draper.iteye.com/blog/2369615
AbstractSelectableChannel定义:http://donald-draper.iteye.com/blog/2369742
NetworkChannel接口定义:http://donald-draper.iteye.com/blog/2369773
ServerSocketChannel定义:http://donald-draper.iteye.com/blog/2369836
Selector定义:http://donald-draper.iteye.com/blog/2370015
在前一篇我们看了一下Selector的定义,今天来看下选择器的基础实现AbstractSelector。
从AbstractSelector的定义可以看出,取消的key放在一个set集合中,对集合进行添加操作时,必须同步取消key set集合。反注册选择key完成的实际工作是,将key,从key对应的通道的选择key数组(这个我们在选择通道相关文章中有讲)中移除。
AbstractInterruptibleChannel接口定义:http://donald-draper.iteye.com/blog/2369238
SelectableChannel接口定义:http://donald-draper.iteye.com/blog/2369317
SelectionKey定义:http://donald-draper.iteye.com/blog/2369499
SelectorProvider定义:http://donald-draper.iteye.com/blog/2369615
AbstractSelectableChannel定义:http://donald-draper.iteye.com/blog/2369742
NetworkChannel接口定义:http://donald-draper.iteye.com/blog/2369773
ServerSocketChannel定义:http://donald-draper.iteye.com/blog/2369836
Selector定义:http://donald-draper.iteye.com/blog/2370015
在前一篇我们看了一下Selector的定义,今天来看下选择器的基础实现AbstractSelector。
package java.nio.channels.spi; import java.io.IOException; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.util.HashSet; import java.util.Set; import sun.nio.ch.Interruptible; import java.util.concurrent.atomic.AtomicBoolean; /** * Base implementation class for selectors. *AbstractSelector为选择器的基础实现类 * <p> This class encapsulates the low-level machinery required to implement * the interruption of selection operations. A concrete selector class must * invoke the {@link #begin begin} and {@link #end end} methods before and * after, respectively, invoking an I/O operation that might block * indefinitely. In order to ensure that the {@link #end end} method is always * invoked, these methods should be used within a * <tt>try</tt> ... <tt>finally</tt> block: <a name="be"> *AbstractSelector封装了系统底层实现选择器中断的操作。在一个可能阻塞的IO操作中, 一个具体的选择器类,必须调用begin在IO操作之前和end方法在IO操作之后,为了确保上述 end方法总是调用,begin和end方法,应该在try,finanlly语句块中使用。 具体使用如下: * <blockquote><pre> * try { * begin(); * // Perform blocking I/O operation here * ... * } finally { * end(); * }</pre></blockquote> * * <p> This class also defines methods for maintaining a selector's * cancelled-key set and for removing a key from its channel's key set, and * declares the abstract {@link #register register} method that is invoked by a * selectable channel's {@link AbstractSelectableChannel#register register} * method in order to perform the actual work of registering a channel. </p> *AbstractSelector提供了维护取消key集合和从通道的选择key集合中移除通道注册到选择器的key的方法 * @author Mark Reinhold * @author JSR-51 Expert Group * @since 1.4 */ public abstract class AbstractSelector extends Selector { private AtomicBoolean selectorOpen = new AtomicBoolean(true); // The provider that created this selector private final SelectorProvider provider; /** * Initializes a new instance of this class. </p> */ protected AbstractSelector(SelectorProvider provider) { this.provider = provider; } //取消的选择key集合 private final Set<SelectionKey> cancelledKeys = new HashSet<SelectionKey>(); //取消选择key void cancel(SelectionKey k) { // package-private synchronized (cancelledKeys) { cancelledKeys.add(k); } } /** * Closes this selector. *关闭选择器 * <p> If the selector has already been closed then this method returns * immediately. Otherwise it marks the selector as closed and then invokes * the {@link #implCloseSelector implCloseSelector} method in order to * complete the close operation. </p> *如果选择器已经关闭,则这个方法将立刻返回。否则标记选择器已关闭,同时调用 #implCloseSelector完成关闭操作。 * @throws IOException * If an I/O error occurs */ public final void close() throws IOException { boolean open = selectorOpen.getAndSet(false); if (!open) return; implCloseSelector(); } /** * Closes this selector. *关闭选择器 * <p> This method is invoked by the {@link #close close} method in order * to perform the actual work of closing the selector. This method is only * invoked if the selector has not yet been closed, and it is never invoked * more than once. *此方法在关闭选择器的方法中,调用完成实际的关闭选择器工作。这个方法在还没有关闭 选择器的时候调用,不会被调用一次以上。 * <p> An implementation of this method must arrange for any other thread * that is blocked in a selection operation upon this selector to return * immediately as if by invoking the {@link * java.nio.channels.Selector#wakeup wakeup} method. </p> *方法的实现必须安排其他阻塞在选择器的选择操作上的线程立即返回,就像调用选择的 唤醒方法一样。 * @throws IOException * If an I/O error occurs while closing the selector */ protected abstract void implCloseSelector() throws IOException; //选择器是否打开 public final boolean isOpen() { return selectorOpen.get(); } /** * Returns the provider that created this channel. * * @return The provider that created this channel */ public final SelectorProvider provider() { return provider; } /** * Retrieves this selector's cancelled-key set. *获取取消key集合,使用时必须同步取消key集合 * <p> This set should only be used while synchronized upon it. </p> * * @return The cancelled-key set */ protected final Set<SelectionKey> cancelledKeys() { return cancelledKeys; } /** * Registers the given channel with this selector. *注册通道到选择器 * <p> This method is invoked by a channel's {@link * AbstractSelectableChannel#register register} method in order to perform * the actual work of registering the channel with this selector. </p> *这个方法在AbstractSelectableChannel#register的方法中调用完成实际的注册通道 到选择器工作。 * @param ch * The channel to be registered * * @param ops * The initial interest set, which must be valid * * @param att * The initial attachment for the resulting key * * @return A new key representing the registration of the given channel * with this selector */ protected abstract SelectionKey register(AbstractSelectableChannel ch, int ops, Object att); /** * Removes the given key from its channel's key set. *反注册,及将选择key从通道的选择key集合中移除。 * <p> This method must be invoked by the selector for each channel that it * deregisters. </p> *注册到选择器的所有通道,在反注册时,必须调用此方法。 * @param key * The selection key to be removed */ protected final void deregister(AbstractSelectionKey key) { ((AbstractSelectableChannel)key.channel()).removeKey(key); } // -- Interruption machinery -- private Interruptible interruptor = null;//中断器 /** * Marks the beginning of an I/O operation that might block indefinitely. *标记不确定阻塞IO操作的开始。 * <p> This method should be invoked in tandem with the {@link #end end} * method, using a <tt>try</tt> ... <tt>finally</tt> block as * shown <a href="#be">above</a>, in order to implement interruption for * this selector. *这个方法应配合end方法在try语句块中使用,在finnally语句块中调用end,这个使用方法 在类的java Doc有说。 * <p> Invoking this method arranges for the selector's {@link * Selector#wakeup wakeup} method to be invoked if a thread's {@link * Thread#interrupt interrupt} method is invoked while the thread is * blocked in an I/O operation upon the selector. 如果线程阻塞在选择器的IO操作上,并且线程中断,调用此方法必须安排选择器 wakeup唤醒线程。 </p> */ protected final void begin() { if (interruptor == null) { interruptor = new Interruptible() { public void interrupt(Thread ignore) { //唤醒等待选择操作的线程 AbstractSelector.this.wakeup(); }}; } AbstractInterruptibleChannel.blockedOn(interruptor); Thread me = Thread.currentThread(); if (me.isInterrupted()) //如果当前线程处于中断状态,则消除中断位 interruptor.interrupt(me); } /** * Marks the end of an I/O operation that might block indefinitely. *标记不确定阻塞IO操作的结束 * <p> This method should be invoked in tandem with the {@link #begin begin} * method, using a <tt>try</tt> ... <tt>finally</tt> block as * shown <a href="#be">above</a>, in order to implement interruption for * this selector. </p> */此方法与begin方法在try,finanlly方法中配合使用,为了实现现在器的中断 protected final void end() { AbstractInterruptibleChannel.blockedOn(null); } }
从AbstractSelector的定义可以看出,取消的key放在一个set集合中,对集合进行添加操作时,必须同步取消key set集合。反注册选择key完成的实际工作是,将key,从key对应的通道的选择key数组(这个我们在选择通道相关文章中有讲)中移除。
发表评论
-
文件通道解析二(文件锁,关闭通道)
2017-05-16 23:17 1075文件通道解析一(读写操作,通道数据传输等):http://do ... -
文件通道解析一(读写操作,通道数据传输等)
2017-05-16 10:04 1172Reference定义(PhantomRefere ... -
文件通道创建方式综述
2017-05-15 17:39 1074Reference定义(PhantomReference,Cl ... -
文件读写方式简单综述后续(文件,流构造)
2017-05-14 23:04 1496Java Socket通信实例:http://donald-d ... -
文件读写方式简单综述
2017-05-14 11:13 1142Java Socket通信实例:http://donald-d ... -
FileChanne定义
2017-05-12 23:28 948文件读写方式简单综述:http://donald-draper ... -
SeekableByteChannel接口定义
2017-05-11 08:43 1245ByteChannel,分散聚集通道接口的定义(SocketC ... -
FileChannel示例
2017-05-11 08:37 1003前面我们看过socket通道,datagram通道,以管道Pi ... -
PipeImpl解析
2017-05-11 08:41 941ServerSocketChannel定义:http://do ... -
Pipe定义
2017-05-10 09:07 918Channel接口定义:http://donald-drape ... -
NIO-Pipe示例
2017-05-10 08:47 916PipeImpl解析:http://donald-draper ... -
DatagramChannelImpl 解析四(地址绑定,关闭通道等)
2017-05-10 08:27 792DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析三(多播)
2017-05-10 08:20 1928DatagramChannelImpl 解析一(初始化):ht ... -
NIO-UDP实例
2017-05-09 12:32 1595DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析二(报文发送与接收)
2017-05-09 09:03 1419DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析一(初始化)
2017-05-08 21:52 1422Channel接口定义:http://donald-drape ... -
MembershipKeyImpl 简介
2017-05-08 09:11 932MembershipKey定义:http://donald-d ... -
DatagramChannel定义
2017-05-07 23:13 1238Channel接口定义:http://donald-drape ... -
MulticastChanne接口定义
2017-05-07 13:45 1148NetworkChannel接口定义:ht ... -
MembershipKey定义
2017-05-06 16:20 927package java.nio.channels; i ...
相关推荐
`AbstractList`、`AbstractListModel`、`AbstractMap`、`AbstractMethodError`、`AbstractPreferences`、`AbstractQueue`、`AbstractQueuedSynchronizer`、`AbstractSelectableChannel`、`AbstractSelector`、`...
Matlab领域上传的视频均有对应的完整代码,皆可运行,亲测可用,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
Matlab领域上传的视频均有对应的完整代码,皆可运行,亲测可用,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
SATA命令协议文档,新手必学,高手必经之路的利器。
白色大气风格响应式产品展示企业网页模板.zip
内容概要:文章介绍了如何使用Python开发一个简单的自动点餐程序。该程序通过显示菜单、接收用户点单输入并记录订单详情,最终计算总费用,展示了基础的交互流程和订单处理方法。 适合人群:Python初学者,对交互式程序设计感兴趣的学习者。 使用场景及目标:适用于希望了解基本交互式应用程序开发流程的新手程序员,目标是掌握Python中的函数定义、数据结构使用、流程控制以及简单的用户交互技术。 阅读建议:跟随程序步骤实践,尝试扩展或优化现有功能,如加入折扣计算、订单存储等功能,以增强理解和编程能力。
白色大气风格的境外游景区模板下载.zip
白色大气风格的商业模板下载.zip
华豫佰佳组合促销视图.sql
白色创意风格的室内装修设计CSS3模板.zip
platform-tools-latest-darwin.zip
CH579 以太网转串口 串口服务器代码 需要自己编程提升能力的非常值得参考的代码 几乎所有的编程思路编程技巧资源都涉及到了,代码简单易懂 ,注释清楚,本代码实现最串口服务器的功能,有电路图。
白色非常简洁的商务网站模板下载.zip
白色大气风格的美食DIY应用APP官网模板.zip
白色大气风格的女性风衣企业网站模板.zip
1. 平台在家电和电子产品方面的营运情况如何? 2. 哪些品牌和类别销量最高? 3. 用户消费规律 4. 哪些是我们的重点用户? 5. 平台有哪些优势和不足,需要如何改进?
白色大气简洁的汽车配件商城整站网站模板下载.zip
白色简洁的数码相机商城整站网站模板下载.zip
白色大气风格的多用途企业网站模板.zip
白色大气风格的恐龙化石博物馆模板下载.zip