正常拷貝
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();
}
}
}
總結
零拷貝技術,減少了上下文切換,減少了複製次數