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. 【C语言】构造长度可变的二维数组

    #include <stdio.h> #include <malloc.h> #include <memory.h> int getArray(int ***p,i ...

  2. C++ new操作符详解

    一.new操作符的概念 我们通常讲的new是指的是new operator,其实还有另外两个概念,operator new 和 placement new. 1.new operator 我们在使用n ...

  3. solr学习之一 搜索基本知识

    学习了一段时间的solr了,用自己的方式总结下目前学到的内容,这是个系列文章,这里面的有些说法可能不准确,也可能有问题 欢迎大家指正. 一.搜索引擎目的 搜索引擎在我们的生活中,已经无处不在,除了我们 ...

  4. css3实现3d显示效果

    <!doctype html><html><head>    <meta charset="UTF-8">    <meta ...

  5. LeetCode: Divide Two Integers 解题报告

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...

  6. .NetCore简介

    引用:https://docs.microsoft.com/zh-cn/dotnet/articles/core/index .NET Core 是一个通用开发平台,由 Microsoft 和 Git ...

  7. Excel最多可存多少行,多少列?

    查到的资料如下: Excel 07-2003一个工作表最多可有65536行,行用数字1—65536表示;最多可有256列,列用英文字母A—Z,AA—AZ,BA—BZ,……,IA—IV表示:一个工作簿中 ...

  8. 【WPF/MVVM】把鼠标事件写到Controller层

    要使用Mouse Event,最快捷的方法便是前台控件直接绑定事件,然后再后台代码中实现. 在MVVM中,View层的后台代码无法调用Contrller层的函数.(反过来可以Controller –& ...

  9. C语言 · 上帝造题五分钟

    算法提高 上帝造题五分钟   时间限制:1.0s   内存限制:256.0MB      问题描述 第一分钟,上帝说:要有题.于是就有了L,Y,M,C 第二分钟,LYC说:要有向量.于是就有了长度为n ...

  10. 数据库——SQL中EXISTS怎么用1(转)

    EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False 方法/步骤   EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返 ...