Java Nio注意事项(未完待续)

1. SelectorKeys迭代器用完移除

Selector.selectKeys()迭代器用完移除,不然会一直select出来

Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> iterator = keys.iterator();
while (iterator.hasNext()) {
           SelectionKey key = iterator.next();
           // 这里要移除,不然会一直select出来
           iterator.remove();
           handleKey(key);
}
2. Key的时间要取消注册

Key的时间要取消注册,不然也会一直select到

if (key.isWritable()) {
	SocketChannel channel = (SocketChannel) key.channel();
    log.info("与server[{}]通道可写", channel.getRemoteAddress());
    // 这里取消key感兴趣的事件,避免写事件一直select到
    key.interestOps(key.readyOps() & ~SelectionKey.OP_WRITE);
}
3. 客户端连接问题

客户端连接服务端,一定要先Selector.open() 再执行 channel.connect(),不然会无法select到connectable事件

SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.bind(new InetSocketAddress(InetAddress.getLocalHost(), 9090));

Selector selector = Selector.open();
socketChannel.connect(new InetSocketAddress(InetAddress.getLocalHost(), 8080));
System.out.println("connect server success....");
socketChannel.register(selector, SelectionKey.OP_CONNECT);
4. 调用attach问题

attach以后还需要register第三个参数传过去,才能在别的渠道用attachment方法获取

selectionKey.attach(attachment);
socketChannel.register(selector, SelectionKey.OP_WRITE, attachment);
5. selector.select(); 调用

selector.select()最好加个时间,比如 selector.select(100); 间隔100ms,select一次。不然会一直阻塞在select那,导致子线程利用socketChannel.register(selector, opt); 无法生效

int select = selector.select(100);
6. 注册事件attachment附件问题
// 清空attachment
socketChannel.register(selector, opt);

// 保留attachment
selectionKey.interestOps(selectionKey.readyOps() | opt)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值