NIO简介

nio三大组件

channel,buffer,selector

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

bytebuffer

        //申请空间
        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对象

channel.read(new ByteBuffer[]{a, b, c});

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

相关推荐

  1. NIO简介

    2024-04-10 11:04:05       38 阅读
  2. 【UML】NO.1 UML简介

    2024-04-10 11:04:05       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-10 11:04:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-10 11:04:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-10 11:04:05       87 阅读
  4. Python语言-面向对象

    2024-04-10 11:04:05       96 阅读

热门阅读

  1. 大语言模型本地化部署思路

    2024-04-10 11:04:05       35 阅读
  2. ARM CoreLink 系列的互连产品包括哪些?

    2024-04-10 11:04:05       31 阅读
  3. vue中使用use引入的svg怎么添加title

    2024-04-10 11:04:05       36 阅读
  4. git-es6-promisem面试

    2024-04-10 11:04:05       31 阅读
  5. ES6 => 箭头函数

    2024-04-10 11:04:05       30 阅读
  6. Hermite 多项式

    2024-04-10 11:04:05       26 阅读
  7. 使用UDP完成网络单词查询,利用dict数据库

    2024-04-10 11:04:05       36 阅读
  8. Request 读取窗体变量四种方式

    2024-04-10 11:04:05       40 阅读
  9. 深度学习神经网络模型微调

    2024-04-10 11:04:05       42 阅读