Java 零拷貝

正常拷貝

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();
        }
    }
}

總結

零拷貝技術,減少了上下文切換,減少了複製次數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章