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的更多相关文章

  1. laravel Event执行顺序

    laravel一大特色就是event事件系统.一般首先要listen一个事件,随后fire那个事件,这时执行路径将会调用event handler,返回后继续执行.例如: Event::listen( ...

  2. Redis的Time Event与File Event的微妙关系

    redis里设计了两类事件,一类是file event,一类是time event. 其中file event主要为网络事件而设计,而time event为一些后台事件设计. 在两类事件的管理设计上, ...

  3. zepto源码学习-04 event

    之前说完$(XXX),然后还有很多零零碎碎的东西需要去分析,结果一看代码,发现zepto的实现都相对简单,没有太多可分析的.直接略过了一些实现,直接研究Event模块,相比JQuery的事件系统,ze ...

  4. Zepto源码分析-event模块

    源码注释 // Zepto.js // (c) 2010-2015 Thomas Fuchs // Zepto.js may be freely distributed under the MIT l ...

  5. 读Zepto源码之Event模块

    Event 模块是 Zepto 必备的模块之一,由于对 Event Api 不太熟,Event 对象也比较复杂,所以乍一看 Event 模块的源码,有点懵,细看下去,其实也不太复杂. 读Zepto源码 ...

  6. 一个普通的 Zepto 源码分析(三) - event 模块

    一个普通的 Zepto 源码分析(三) - event 模块 普通的路人,普通地瞧.分析时使用的是目前最新 1.2.0 版本. Zepto 可以由许多模块组成,默认包含的模块有 zepto 核心模块, ...

  7. zepto源码分析·event模块

    准备知识 事件的本质就是发布/订阅模式,dom事件也不例外:先简单说明下发布/订阅模式,dom事件api和兼容性 发布/订阅模式 所谓发布/订阅模式,用一个形象的比喻就是买房的人订阅楼房消息,售楼处发 ...

  8. jQuery.event.move

    http://stephband.info/jquery.event.move/ Move events movestart Fired following touchmove or mousemov ...

  9. Bootstrap 4 中 Alerts 实现

    Alert 的使用说明 http://v4-alpha.getbootstrap.com/components/alerts/ JavaScript behavior Triggers Enable ...

随机推荐

  1. 超烂的ELK之filebeat读取【已解决】

    搞了无数次的filebeat-->logstash今天栽了跟头 filebeat在读取如下文件的时候,openchgw.log 软连接speechgw.log.20170703183729文件 ...

  2. python学习笔记(14)--爬虫下载漫画图片修改版

    说明: 1. 2017.3.12,周六从中午吃完包子12点多折腾了一下午加一个晚上,试了4个网站的爬虫,发现都不能下载!甚至前几天测试能下载的都不能用了! 2. 到晚上发现煎蛋网的可以用了,立即试了下 ...

  3. 基于CSS3自定义美化复选框Checkbox组合

    今天我们要来分享一组非常漂亮的CSS3自定义复选框checkbox,每一个checkbox都有其各自的特点.有几款checkbox在选中的情况下还会出现动画效果,非常不错的CSS3自定义美化check ...

  4. ContikiMAC RDC协议

    http://www.mamicode.com/info-detail-1348767.html

  5. Tomcat配置 设置启动参数,点击startup.bat启动

    catalina.batrem ---------------------------------------------------------------------------rem Set J ...

  6. JavaScriptSerializer 时间格式化

    时间格式化 Model m = , Dt = DateTime.Now }; JavaScriptSerializer js = new JavaScriptSerializer(); string ...

  7. VS2010在C#头文件中添加文件注释的方法(转)

    步骤: 1.VS2010 中找到(安装盘符以D盘为例)D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTempl ...

  8. StarRTC , AndroidThings , 树莓派小车,公网环境,视频遥控(三)手机端

    原文地址:http://blog.starrtc.com/?p=111 这篇来介绍一下整个项目的手机端部分.在上一篇里我们已经将sdk导入到项目中了,下边直接用即可. 1 登录StarRTC的服务跟小 ...

  9. kettle启动时候报a fatal exception has occurred

    本人刚接触ETL工具 Data Integration - Kettle ,下载了kettle6.0版本,但是在window +jdk1.7(32位)下启动报错, 使用SpoonDebug.bat写的 ...

  10. Web下的整体测试 --性能测试及优化思路

    随着Internet的日益普及,现在基于B/S结构的大型应用越来越多,可如何对这些应用进行测试成为日益迫切的问题.有许多测试人员来信问我B/S的测试如何做,由于工作较繁忙,对大家提出的问题也是头痛医头 ...