请教JAVA NIO

Helprog 2018-08-04 10:26:49

Selector selector = Selector.open();
ServerSocketChannel channel = ServerSocketChannel.open();
channel.configureBlocking(false); //讲通道设置为非阻塞
channel.socket().bind(new InetSocketAddress(PORT)); //绑定端口
channel.register(this.selector, SelectionKey.OP_ACCEPT); //为通道注册OP_ACCEPT事件

在上述代码中,channel注册SelectionKey.OP_ACCEPT、SelectionKey.OP_READ、SelectionKey.OP_WRITE后,它们三分别因什么条件而触发呢?
若channel为SocketChannel,注册成SelectionKey.OP_CONNECT,又会因什么而触发呢?(我在一些博客的代码中看到注册成这个事件并不会连接,而是通知selector这个channel需要注册了)。 若能解答感激不尽!!!
...全文
74 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧