正常拷贝
Kafka 消费者消费数据,从文件中读数据流程
产生4次上下文切换,4次数据复制
使用零拷贝
2次上下文,3次复制
Java示例程序
public class Copy {
public static void main(String[] args) {
File file = new File("/xx/te.txt");
try (FileInputStream fileOutputStream = new FileInputStream(file)) {
FileChannel channel = fileOutputStream.getChannel();
FileOutputStream out = new FileOutputStream("//xx/te1.txt");
FileChannel outChannel = out.getChannel();
//核心从一个管道直接到另一个管道
channel.transferTo(0, file.length(), outChannel);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
总结
零拷贝技术,减少了上下文切换,减少了复制次数