JAVA NIO Selector Channel
- These four events are represented by the four
SelectionKeyconstants:
- SelectionKey.OP_CONNECT
- SelectionKey.OP_ACCEPT
- SelectionKey.OP_READ
- 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 theChannel, via theSelector. There are four different events you can listen for:- Connect
- Accept
- Read
- 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的更多相关文章
- (四:NIO系列) Java NIO Selector
出处:Java NIO Selector 1.1. Selector入门 1.1.1. Selector的和Channel的关系 Java NIO的核心组件包括: (1)Channel(通道) (2) ...
- Java NIO——Selector机制源码分析---转
一直不明白pipe是如何唤醒selector的,所以又去看了jdk的源码(openjdk下载),整理了如下: 以Java nio自带demo : OperationServer.java Oper ...
- Java NIO Selector选择器
Selector是Java NIO中的一个组件,用于检查一个或多个NIO Channel的状态是否处于可读.可写.如此可以实现单线程管理多个channels,也就是可以管理多个网络链接. 为什么使用S ...
- java nio之channel
一.通道(Channel):由 java.nio.channels 包定义的.Channel 表示 IO 源与目标打开的连接.Channel 类似于传统的“流”.只不过 Channel本身不能直接访问 ...
- Java NIO -- 通道 Channel
通道(Channel):由 java.nio.channels 包定义的.Channel 表示 IO 源与目标打开的连接.Channel 类似于传统的“流”.只不过 Channel本身不能直接访问数据 ...
- Java NIO 之 Channel(通道)
历史回顾: Java NIO 概览 Java NIO 之 Buffer(缓冲区) 其他高赞文章: 面试中关于Redis的问题看这篇就够了 一文轻松搞懂redis集群原理及搭建与使用 一 Channel ...
- 【Java nio】Channel
package com.slp.nio; import org.junit.Test; import java.io.*; import java.nio.ByteBuffer; import jav ...
- JAVA NIO 之Channel
缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.Channel 通道就是将数据传输给 ByteBuffer 对象或者从 ByteBuffer 对象获取数据进行传输. Channel 用于在 ...
- Java NIO教程 Channel
Channel是一个连接到数据源的通道.程序不能直接用Channel中的数据,必须让Channel与BtyeBuffer交互数据,才能使用Buffer中的数据. 我们用FileChannel作为引子, ...
随机推荐
- 第十五章、Model/View架构中Item Views部件的父类
老猿Python博文目录 老猿Python博客地址 引言:本章早就写好了,其简版<第15.18节 PyQt(Python+Qt)入门学习:Model/View架构中视图Item Views父类详 ...
- PyQt学习随笔:QStandardItemModel使用注意事项
老猿Python博文目录 老猿Python博客地址 在使用QStandardItemModel或其派生类作为view对象的数据存储时,有如下几点需要注意: 1.如果是多行多列的数据存储,对应视图如果没 ...
- 开源脉冲神经网络深度学习框架——惊蛰(SpikingJelly)
开源脉冲神经网络深度学习框架--惊蛰(SpikingJelly) 背景 近年来神经形态计算芯片发展迅速,大量高校企业团队跟进,这样的芯片运行SNN的能效比与速度都超越了传统的通用计算设备.相应的,神经 ...
- Java程序员需要了解的底层知识(一)
硬件基础知识 - Java相关硬件 汇编语言的执行过程(时钟发生器 寄存器 程序计数器) 计算机启动过程 进程线程纤程的基本概念面试高频 - 纤程的实现 内存管理 进程管理与线程管理(进程与线程 ...
- hiveSQL和MySQL区别
1.hive支持按行分割,按字段分割,如按','分割: lateral view explode(split( , ',')) 2.hive不支持等值连接,即不支持where a.id = b.id的 ...
- window下使用cmd查看端口占用的进程,并杀死该进程
做项目的时候经常会遇到"address already in use"的情况,此时可以选择使用dos命令将该进程杀死. 首先,查找端口对应的进程,使用命令(以进程号8080为例): ...
- mysql 8.0 MGR组复制配置
一.配置组复制的步骤 1.初始化数据目录 2.配置主服务器(primary) 3.配置辅助服务器(secondaries) 4.启动mysql实例 5.安装组复制插件(primary and seco ...
- Unity2D 人物移动切换人物图片
勾选Constraints_freeze Rotation_z轴锁定,防止碰撞偏移. public float moveSpeed = 3f;//定义移动速度 priv ...
- Kubernetes【K8S】(二):搭建Kubernetes环境
系统初始化 设置系统时区 # 设置系统时区为 亚洲/上海 [root@k8s-master01 ~]# timedatectl set-timezone Asia/Shanghai # 设置当前得UT ...
- wpa_supplicant 检测错误密码
选好了 wifi ssid,填了密码,生成新配置文件,重启了wpa_supplicant,怎么知道输入的密码对不对,如果不对有什么体现? wpa_supplicant 前台运行时,打印信息中会有: W ...