NIO简介

nio三大组件

channel,buffer,selector

channel为双向输入输出通道,buffer为缓存,selector为选择器,通过selector来选择线程对出现io操作的channel服务,可有有效的增加线程的工作效率,不用等待某个连接断开才释放线程

bytebuffer

java 复制代码
        //申请空间
        ByteBuffer byteBuffer = ByteBuffer.allocate(100);
        //获取通道
        FileChannel channel = new FileInputStream().getChannel();
        //循环读取
        while (true){
            int read = channel.read(byteBuffer);
            if (read == -1) break;
            //切换读模式
            byteBuffer.flip();
            while (byteBuffer.hasRemaining()){
                byte b = byteBuffer.get();
            }
            //切换写模式
            byteBuffer.clear();
        }

buffer写入数据可以使用

  • 调用 channel 的 read
  • 调用 buffer 自己的 put

buffer读数据有write和get

可以通过这样的方式填充多个buffer,abc为三个buffer对象

java 复制代码
channel.read(new ByteBuffer[]{a, b, c});

同理也可以使用put和write读出写入channel

相关推荐
huangdong_1 小时前
1688商品图片采集技术解析:登录态处理与SKU图自动分类
开发语言
马士兵教育1 小时前
Java还有前景吗?Java+AI大模型学习路线及项目?
java·人工智能·python·学习·机器学习
chase_my_dream1 小时前
C++ + SLAM 高频面试问题整理
开发语言·c++·面试
snow@li1 小时前
Java:理解 Gradle / 后端项目的管家 / 打包SpringBoot 应用 / 完成编译、下载依赖、运行测试、打包 JAR/WAR / 速查表
java
Cloud_Shy6181 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 30 - 32)
开发语言·人工智能·笔记·python·学习方法
云烟成雨TD1 小时前
Spring AI 1.x 系列【57】动态工具发现:Tool Search Tool
java·人工智能·spring
zfoo-framework2 小时前
[修改代码使用]codex官方app中使用中转(不需要cc-switch) 1.config.toml 2.sk方式登录
java
天佑木枫2 小时前
15天Python入门系列 · 序
开发语言·python
逍遥德2 小时前
MQTT教程详解-05.SpringBoot集成mqtt client 性能分析
java·spring boot·spring·mt
云烟成雨TD2 小时前
Spring AI 1.x 系列【54】Retry 机制分析
java·人工智能·spring