//管道是两个线程之间的单向数据传输
public void test() throws IOException{
//1.获取管道
Pipe pipe = Pipe.open();
//2.将缓冲区中的数据写入管道
ByteBuffer buf = ByteBuffer.allocate(1024);
Pipe.SinkChannel sinkChannel = pipe.sink();
buf.put("通过单项管道发送数据".getBytes());
buf.flip();
sinkChannel.write(buf);
//3.获取缓冲区的数据
Pipe.SourceChannel sourceChannel = pipe.source();
buf.flip();
int len = sourceChannel.read(buf);
System.out.println(new String(buf.array(),0,len));
sourceChannel.close();
sinkChannel.close();
}Nio利用管道Pipe实现单向数据传输
最新推荐文章于 2019-10-05 23:04:29 发布
本文介绍了一个使用Java NIO管道(Pipe)进行线程间通信的例子。演示了如何通过SinkChannel写入数据,并通过SourceChannel读取数据。
649

被折叠的 条评论
为什么被折叠?



