同时读取文件(首选java)

use*_*593 24 java io concurrency file

我有一个需要几个小时才能处理的大文件.所以我正在考虑尝试估计块并且并行读取块.

是否可以在单个文件上并发读取?我既看了两个RandomAccessFile,nio.FileChannel但基于其他帖子我不确定这种方法是否有效.

Pet*_*lák 19

这里最重要的问题是你的案例中的瓶颈什么.

如果瓶颈是您的磁盘IO,那么您在软件部分可以做的事情就不多了.并行化计算只会使事情变得更糟,因为同时从不同部分读取文件会降低磁盘性能.

如果瓶颈是处理能力,并且您有多个CPU核心,那么您可以利用启动多个线程来处理文件的不同部分.您可以安全地创建多个InputStreams或Readers来并行读取文件的不同部分(只要您没有超过操作系统对打开文件数量的限制).您可以将工作分成任务并并行运行,如下例所示:

import java.io.*;
import java.util.*;
import java.util.concurrent.*;

public class Split {
    private File file;

    public Split(File file) {
        this.file = file;
    }

    // Processes the given portion of the file.
    // Called simultaneously from several threads.
    // Use your custom return type as needed, I used String just to give an example.
    public String processPart(long start, long end)
        throws Exception
    {
        InputStream is = new FileInputStream(file);
        is.skip(start);
        // do a computation using the input stream,
        // checking that we don't read more than (end-start) bytes
        System.out.println("Computing the part from " + start + " to " + end);
        Thread.sleep(1000);
        System.out.println("Finished the part from " + start + " to " + end);

        is.close();
        return "Some result";
    }

    // Creates a task that will process the given portion of the file,
    // when executed.
    public Callable<String> processPartTask(final long start, final long end) {
        return new Callable<String>() {
            public String call()
                throws Exception
            {
                return processPart(start, end);
            }
        };
    }

    // Splits the computation into chunks of the given size,
    // creates appropriate tasks and runs them using a 
    // given number of threads.
    public void processAll(int noOfThreads, int chunkSize)
        throws Exception
    {
        int count = (int)((file.length() + chunkSize - 1) / chunkSize);
        java.util.List<Callable<String>> tasks = new ArrayList<Callable<String>>(count);
        for(int i = 0; i < count; i++)
            tasks.add(processPartTask(i * chunkSize, Math.min(file.length(), (i+1) * chunkSize)));
        ExecutorService es = Executors.newFixedThreadPool(noOfThreads);

        java.util.List<Future<String>> results = es.invokeAll(tasks);
        es.shutdown();

        // use the results for something
        for(Future<String> result : results)
            System.out.println(result.get());
    }

    public static void main(String argv[])
        throws Exception
    {
        Split s = new Split(new File(argv[0]));
        s.processAll(8, 1000);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我能够将文件分块并同时读取它.对于.5GB文本文件,这里是我的结果(hh.mm.ss.SSS):chunks = [1]:0:18:10.328 chunks = [2]:0:13:19.125 chunks = [3]:0: 12:54.824.差别不大.但对我来说,最好的解决方案是压缩文件并连续处理zip文件.这是因为高压缩比.zip文件最终为10MB (2认同)

Pet*_*rey 8

如果您有多个独立的spindal,您可以并行读取大文件.例如,如果您有一个Raid 0 + 1剥离文件系统,您可以通过触发对同一文件的多个并发读取来查看性能改进.

但是,如果您有一个组合文件系统,如Raid 5或6或普通单个磁盘.按顺序读取文件很可能是从该磁盘读取的最快方法.注意:操作系统非常智能,可以在它看到您正在按顺序读取时预读取读取,因此使用其他线程执行此操作不太可能有所帮助.

即使用多个线程不会让你的磁盘更快.

如果您想更快地从磁盘读取,请使用更快的驱动器.典型的SATA HDD可以读取大约60 MB /秒并执行120 IOPS.典型的SATA SSD驱动器可以以大约400 MB/s的速度读取并执行80,000 IOPS,典型的PCI SSD可以以900 MB/s的速度读取并执行230,000 IOPS.


Buh*_*uhb 2

如果您正在从硬盘驱动器读取文件,那么获取数据的最快方法是从头到尾读取文件,即不是同时读取。

现在,如果处理需要时间,那么让多个线程同时处理不同的数据块可能会受益,但这与您读取文件的方式无关。