when an event of selector will be fired
OP_READ
Operation-set bit for read operations.
Suppose that a selection key's interest set contains OP_READ at the start of a selection operation. If the selector detects that the corresponding channel is ready for reading, has reached end-of-stream, has been remotely shut down for further reading, or has an error pending, then it will add OP_READ to the key's ready-operation set and add the key to its selected-key set.
OP_WRITE
写就绪相对有一点特殊,一般来说,你不应该注册写事件。写操作的就绪条件为底层缓冲区有空闲空间,而写缓冲区绝大部分时间都是有空闲空间的,所以当你注册写事件后,写操作一直是就绪的,选择处理线程全占用整个CPU资源。所以,只有当你确实有数据要写时再注册写操作,并在写完以后马上取消注册。
当有数据在写时,将数据写到缓冲区中,并注册写事件。
public void write(byte[] data) throws IOException {
writeBuffer.put(data);
key.interestOps(SelectionKey.OP_WRITE);
}
注册写事件后,写操作就绪,这时将之前写入缓冲区的数据写入通道,并取消注册。
channel.write(writeBuffer);
key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
set OP_WRITE Only when you have data ready
A common mistake is to enable OP_WRITE on a selection key and leave it set. This results in the selecting thread spinning because 99% of the time a socket channel is ready for writing. In fact the only times it's not going to be ready for writing is during connection establishment or if the local OS socket buffer is full. The correct way to do this is to enable OP_WRITE only when you have data ready to be written on that socket channel. And don't forget to do it from within the selecting thread.
只有数据可以写时才注册OP_WRITE操作。
避免死锁的一个简单方法就是不要在同一个socket同时注册多个操作。
when an event of selector will be fired的更多相关文章
- laravel Event执行顺序
laravel一大特色就是event事件系统.一般首先要listen一个事件,随后fire那个事件,这时执行路径将会调用event handler,返回后继续执行.例如: Event::listen( ...
- Redis的Time Event与File Event的微妙关系
redis里设计了两类事件,一类是file event,一类是time event. 其中file event主要为网络事件而设计,而time event为一些后台事件设计. 在两类事件的管理设计上, ...
- zepto源码学习-04 event
之前说完$(XXX),然后还有很多零零碎碎的东西需要去分析,结果一看代码,发现zepto的实现都相对简单,没有太多可分析的.直接略过了一些实现,直接研究Event模块,相比JQuery的事件系统,ze ...
- Zepto源码分析-event模块
源码注释 // Zepto.js // (c) 2010-2015 Thomas Fuchs // Zepto.js may be freely distributed under the MIT l ...
- 读Zepto源码之Event模块
Event 模块是 Zepto 必备的模块之一,由于对 Event Api 不太熟,Event 对象也比较复杂,所以乍一看 Event 模块的源码,有点懵,细看下去,其实也不太复杂. 读Zepto源码 ...
- 一个普通的 Zepto 源码分析(三) - event 模块
一个普通的 Zepto 源码分析(三) - event 模块 普通的路人,普通地瞧.分析时使用的是目前最新 1.2.0 版本. Zepto 可以由许多模块组成,默认包含的模块有 zepto 核心模块, ...
- zepto源码分析·event模块
准备知识 事件的本质就是发布/订阅模式,dom事件也不例外:先简单说明下发布/订阅模式,dom事件api和兼容性 发布/订阅模式 所谓发布/订阅模式,用一个形象的比喻就是买房的人订阅楼房消息,售楼处发 ...
- jQuery.event.move
http://stephband.info/jquery.event.move/ Move events movestart Fired following touchmove or mousemov ...
- Bootstrap 4 中 Alerts 实现
Alert 的使用说明 http://v4-alpha.getbootstrap.com/components/alerts/ JavaScript behavior Triggers Enable ...
随机推荐
- 怎么查看eclipse是否支持maven
打开eclipse,选择Windows->Preferences 查看Preferences下是否有Maven即可
- EXCEL多列名称组合成一列名称
- HTML5使用详解
1.什么是HTML5 HTML5是新的HTML标准. 支持最新的Safari,Chrome,Firefox以及Opera,Ie9支持某些HTML5特性. 2.新建HTML5页面 <!DOCTYP ...
- 深入理解php 匿名函数和 Closure
而在PHP 5.3发布的时候, 其中有一条new feature就是支持闭包/Lambda Function, 我第一反应是以为zval新增了一个IS_FUNCTION, 但实际上是构造了一个PHP ...
- VC++编程之道读书笔记(2)
第三篇 技术细节 第七章:细说开发人员必知必会的39个开发细节 细节36:单例模式的应用 在开发程序时,往往需要在整个工程中只需要一个类的实例.而这个实例一旦被创建就不能被其他的实例再创建了,通常我们 ...
- const在c和c++中地位不同
先测试C语言的const: #include<stdio.h> int main() { ; //等价于 //int const a = 10; //a = 11;//err ; cons ...
- addEventListener()绑定事件的对象方法。
addEventListener()绑定事件的对象方法.addEventListener()含有三个参数,一个是事件名称,另一个是事件执行的函数,最后一个是事件捕获,, obj.addEventLis ...
- perl 计算方差中值平均数 Statistics::Descriptive;
http://search.cpan.org/~shlomif/Statistics-Descriptive-3.0612/lib/Statistics/Descriptive.pm use Stat ...
- 深入理解Java虚拟机 -- 读书笔记(1):JVM运行时数据区域
深入理解Java虚拟机 -- 读书笔记:JVM运行时数据区域 本文转载:http://blog.csdn.net/jubincn/article/details/8607790 本系列为<深入理 ...
- iOS边练边学--(Quartz2D)图片添加水印
一.给图片添加水印的基本步骤 加载图片 手动创建位图上下文 绘制原生的图片 给原生的图片添加文字 生成一张图片给我们,从上下文中获取图片 关闭上下文 二.注意:位图上下文的获取方式跟layer上下文不 ...