//管道是两个线程之间的单向数据传输
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-08-08 19:31:32 发布