• These four events are represented by the four SelectionKey constants:
  1. SelectionKey.OP_CONNECT
  2. SelectionKey.OP_ACCEPT
  3. SelectionKey.OP_READ
  4. SelectionKey.OP_WRITE
  • Notice the second parameter of the register() method. This is an "interest set", meaning what events you are interested in listening for in the Channel, via the Selector. There are four different events you can listen for:

    1. Connect
    2. Accept
    3. Read
    4. Write
  • SelectionKey

As you saw in the previous section, when you register a Channel with a Selector the register() method returns a SelectionKey objects. This SelectionKey object contains a few interesting properties:

(如上一节中所见,当使用Selector注册Channel时,register()方法将返回SelectionKey对象。这个SelectionKey对象包含一些有趣的属性)

  • The interest set
  • The ready set
  • The Channel
  • The Selector
  • An attached object (optional)

Here is a full example which opens a Selector, registers a channel with it (the channel instantiation is left out), and keeps monitoring the Selector for "readiness" of the four events (accept, connect, read, write).

这是一个完整的示例,该示例打开一个选择器,向其注册一个通道(忽略通道实例化),并继续监视选择器以了解四个事件(接受,连接,读取,写入)的“就绪”状态


Selector selector = Selector.open(); channel.configureBlocking(false); SelectionKey key = channel.register(selector, SelectionKey.OP_READ); while(true) { int readyChannels = selector.selectNow(); if(readyChannels == 0) continue; Set<SelectionKey> selectedKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); while(keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); if(key.isAcceptable()) {
// a connection was accepted by a ServerSocketChannel.(ServerSocketChannel接受了连接) } else if (key.isConnectable()) {
// a connection was established with a remote server(与远程服务器建立连接). } else if (key.isReadable()) {
// a channel is ready for reading(channel已准备就绪,可以阅读) } else if (key.isWritable()) {
// a channel is ready for writing
} keyIterator.remove();
}
}

选择器完成后,将调用其close()方法。这将关闭选择器,并使在此选择器中注册的所有SelectionKey实例无效。通道本身未关闭


参考链接
http://tutorials.jenkov.com/java-nio/selectors.html

JAVA NIO Selector Channel的更多相关文章

  1. (四:NIO系列) Java NIO Selector

    出处:Java NIO Selector 1.1. Selector入门 1.1.1. Selector的和Channel的关系 Java NIO的核心组件包括: (1)Channel(通道) (2) ...

  2. Java NIO——Selector机制源码分析---转

    一直不明白pipe是如何唤醒selector的,所以又去看了jdk的源码(openjdk下载),整理了如下: 以Java nio自带demo : OperationServer.java   Oper ...

  3. Java NIO Selector选择器

    Selector是Java NIO中的一个组件,用于检查一个或多个NIO Channel的状态是否处于可读.可写.如此可以实现单线程管理多个channels,也就是可以管理多个网络链接. 为什么使用S ...

  4. java nio之channel

    一.通道(Channel):由 java.nio.channels 包定义的.Channel 表示 IO 源与目标打开的连接.Channel 类似于传统的“流”.只不过 Channel本身不能直接访问 ...

  5. Java NIO -- 通道 Channel

    通道(Channel):由 java.nio.channels 包定义的.Channel 表示 IO 源与目标打开的连接.Channel 类似于传统的“流”.只不过 Channel本身不能直接访问数据 ...

  6. Java NIO 之 Channel(通道)

    历史回顾: Java NIO 概览 Java NIO 之 Buffer(缓冲区) 其他高赞文章: 面试中关于Redis的问题看这篇就够了 一文轻松搞懂redis集群原理及搭建与使用 一 Channel ...

  7. 【Java nio】Channel

    package com.slp.nio; import org.junit.Test; import java.io.*; import java.nio.ByteBuffer; import jav ...

  8. JAVA NIO 之Channel

    缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.Channel 通道就是将数据传输给 ByteBuffer 对象或者从 ByteBuffer 对象获取数据进行传输. Channel 用于在 ...

  9. Java NIO教程 Channel

    Channel是一个连接到数据源的通道.程序不能直接用Channel中的数据,必须让Channel与BtyeBuffer交互数据,才能使用Buffer中的数据. 我们用FileChannel作为引子, ...

随机推荐

  1. C#Excel导出注意事项

    Excel 导出 1.首先在服务器中安装office ,并且要注册2.在组件服务中 设置Microsoft.excel.appliction 属性中设置自定义加network service用户并交互 ...

  2. 通过sql 语句将多行数据拼接到一行中

  3. 20200116_centos7.2 下 mysql_5.7修改root密码

    1. 需改my.cnf文件 [root@rakinda-iot-platform ~]# vim /etc/my.cnf 2. 新增一行, 登录时跳过密码, 保存后退出, 重启mysql system ...

  4. day1(Django)

    1,web项目工作流程 1.1 了解web程序工作流程 1.2 django生命周期   2,django介绍 目的:了解Django框架的作用和特点作用: 简便.快速的开发数据库驱动的网站 Djan ...

  5. Linux下netstat命令简单操作

    netstat -t :TCP协议 -u :UDP协议 -l :监听 -r :路由 -n :显示IP地址和端口号 常用: netstat -tlun 查看本机监听的端口 netstat -an 查看本 ...

  6. 数位DP复习笔记

    前言 复习笔记第五篇.(由于某些原因(见下),放到了第六篇后面更新)CSP-S RP++. luogu 的难度评级完全不对,所以换了顺序,换了别的题目.有点乱,见谅.要骂就骂洛谷吧,原因在T2处 由于 ...

  7. 【C++】C++ new和malloc到底哪里不一样

    作者:李春港 出处:https://www.cnblogs.com/lcgbk/p/14118782.html 目录 一.前言 二.new和malloc两者的区别 2.1 属性的区别 2.2 使用上的 ...

  8. CentOS配置Nginx官方的Yum源

    由于yum源中没有我们想要的nginx,那么我们就需要创建一个"/etc/yum.repos.d/nginx.repo"的文件,其实就是新增一个yum源. [root@niaoyu ...

  9. 面试官:小伙子,你给我简单说一下RocketMQ 整合 Spring Boot吧

    前言 在使用SpringBoot的starter集成包时,要特别注意版本.因为SpringBoot集成RocketMQ的starter依赖是由Spring社区提供的,目前正在快速迭代的过程当中,不同版 ...

  10. js常见正则表达式

    常见正则匹配表达式 export let rege = { //验证URL isUrl: (val) => { if (val == null || val == "") r ...