4.4 处理 read 事件

简介: 4.4 处理 read 事件

@Slf4j public class ChannelDemo6 { public static void main(String[] args) { try (ServerSocketChannel channel = ServerSocketChannel.open()) { channel.bind(new InetSocketAddress(8080)); System.out.println(channel); Selector selector = Selector.open(); channel.configureBlocking(false); channel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            int count = selector.select();

//                int count = selector.selectNow(); log.debug("select count: {}", count); //                if(count <= 0) { //                    continue; //                }


作者:用户5488193880519

链接:https://juejin.cn/post/7282603912649375744

来源:稀土掘金

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

            // 获取所有事件
            Set<SelectionKey> keys = selector.selectedKeys();
            // 遍历所有事件,逐一处理
            Iterator<SelectionKey> iter = keys.iterator();
            while (iter.hasNext()) {
                SelectionKey key = iter.next();
                // 判断事件类型
                if (key.isAcceptable()) {
                    ServerSocketChannel c = (ServerSocketChannel) key.channel();
                    // 必须处理
                    SocketChannel sc = c.accept();
                    sc.configureBlocking(false);
                    sc.register(selector, SelectionKey.OP_READ);
                    log.debug("连接已建立: {}", sc);
                } else if (key.isReadable()) {
                    SocketChannel sc = (SocketChannel) key.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(128);
                    int read = sc.read(buffer);
                    if(read == -1) {
                        key.cancel();
                        sc.close();
                    } else {
                        buffer.flip();
                        debug(buffer);
                    }
                }
                // 处理完毕,必须将事件移除
                iter.remove();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

} 开启两个客户端,修改一下发送文字,输出

sun.nio.ch.ServerSocketChannelImpl[/0:0:0:0:0:0:0:0:8080] 21:16:39 [DEBUG] [main] c.i.n.ChannelDemo6 - select count: 1 21:16:39 [DEBUG] [main] c.i.n.ChannelDemo6 - 连接已建立: java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:60367] 21:16:39 [DEBUG] [main] c.i.n.ChannelDemo6 - select count: 1 +-------------------------------------------------+ |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f | +--------+-------------------------------------------------+----------------+ |00000000| 68 65 6c 6c 6f                                  |hello           | +--------+-------------------------------------------------+----------------+ 21:16:59 [DEBUG] [main] c.i.n.ChannelDemo6 - select count: 1 21:16:59 [DEBUG] [main] c.i.n.ChannelDemo6 - 连接已建立: java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:60378] 21:16:59 [DEBUG] [main] c.i.n.ChannelDemo6 - select count: 1 +-------------------------------------------------+ |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f | +--------+-------------------------------------------------+----------------+ |00000000| 77 6f 72 6c 64                                  |world           | +--------+-------------------------------------------------+----------------+



目录
相关文章
|
7月前
|
人工智能 分布式计算 DataWorks
大数据& AI 产品月刊【2025年1、2月】
大数据& AI 产品技术月刊【2025年1、2月】,涵盖双月技术速递、产品和功能发布、市场和客户应用实践等内容,帮助您快速了解阿里云大数据& AI 方面最新动态。
|
Prometheus 监控 Cloud Native
应用程序部署
应用程序部署
311 3
|
存储 关系型数据库 MySQL
【阿里规约】阿里开发手册解读——数据库和ORM篇
从命名规范、建表规范、查询规范、索引规范、操作规范等角度出发,详细阐述MySQL数据库使用过程中所需要遵循的各种规范。
【阿里规约】阿里开发手册解读——数据库和ORM篇
|
算法
无损加速最高5x,EAGLE-2让RTX 3060的生成速度超过A100
【8月更文挑战第5天】EAGLE-2是一种针对大型语言模型(LLMs)的无损加速算法,通过上下文感知的动态草稿树技术显著提升推理速度。它利用小型模型快速生成草稿,并依据置信度动态调整草稿树结构以提高标记接受率。实验表明EAGLE-2在多种任务上实现2.5x至5x的加速比,且不影响生成质量。相较于其他加速方法,EAGLE-2更高效可靠。[论文链接: https://arxiv.org/pdf/2406.16858]
266 11
|
机器学习/深度学习 人工智能 自然语言处理
大语言模型定义、概念介绍
大语言模型定义、概念介绍
|
缓存 Java API
【Azure 服务总线】详解Azure Service Bus SDK中接收消息时设置的maxConcurrentCalls,prefetchCount参数
【Azure 服务总线】详解Azure Service Bus SDK中接收消息时设置的maxConcurrentCalls,prefetchCount参数
170 0
|
JavaScript API 开发者
Vue3有哪些常用的API
Vue3有哪些常用的API
211 1
|
存储 缓存 索引
数据结构——顺序表的概念和基本操作(超全超详细)
数据结构——顺序表的概念和基本操作(超全超详细)
|
缓存 安全 网络协议
什么是 Proxy?
什么是 Proxy?
2911 0