设为首页 加入收藏

TOP

Java I/O(2):NIO中的Channel(二)
2023-07-25 21:24:46 】 浏览:65
Tags:Java I/O NIO Channel
nnel的数据发给多个Buffer(分散),而Gather则是把多个Buffer的数据发给单个Channel(聚集),就像这样:

 

 

同样可以用代码来演示一下:

// Scattering reads分散过程
ByteBuffer buffer1 = ByteBuffer.allocate(1024);
ByteBuffer buffer2 = ByteBuffer.allocate(1024);
ByteBuffer[] bufferArray1 = { buffer1, buffer2 };
FileChannel channel1 = new FileInputStream("/testfile1").getChannel();
channel1.read(bufferArray1);

// Gathering writes聚集过程
ByteBuffer buffer3 = ByteBuffer.allocate(1024);
ByteBuffer buffer4 = ByteBuffer.allocate(1024);
ByteBuffer[] bufferArray2 = { buffer1, buffer2 };
FileChannel channel2 = new FileInputStream("/testfile1").getChannel();
channel2.write(bufferArray2);

 

 

好了,NIO也属于Java中比较重要的内容,说多了容易搞晕。慢慢来~

 


 

感谢您抽空品鉴!技术、产品、运营和管理问题,可随时留言私信,欢迎骚扰~

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇分布式理论----CAP理论与Base理论 下一篇SpringBoot自定义Banner信息

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目