java – 在多线程应用程序中划分大文件?

我在面试中被问到这个基本问题,我无法回答:

How can we divide a large file between multi threads to process it? If
we are running a multi threaded application and input is a large file
and we have to provide each thread a part of the file to make it fast.
How we can achieve it in Java?

任何人都可以解释我们将如何做到这一点?任何例子也将不胜感激.

最佳答案 获得3个起始位置

    File f = new File("xxx");
    long size = f.length();
    long p1 = 0;
    long p2 = f.length() / 3;
    long p3 = p2 + f.length() / 3;

传递位置和长度以读取线程,将文件移动到位置并读取n个字节

    FileInputStream is = new FileInputStream("xxx");
    FileChannel c= is.getChannel();
    c.position(position);
    // read ...
点赞