How to read large files in Java as blocks without being blocked?

Suppose you have a larger file and you have memory to process it You want to read n-byte files in turn and will not be blocked in the process

>Read a block > pass it to a post > read another block > pass it to a post

I've tried different things and achieved different successes, but blocking always seems to be a problem

Please provide an example of a non blocking way to obtain access, such as byte []

Solution

You can't

You will always block while waiting for the disk to provide you with data If you have a lot of work to do for each data block, it may be helpful to use a second thread: the thread can perform CPU intensive work on the data, while the first thread is blocked and waits for the next read to complete

But that doesn't sound like you

Your best choice is to read as much data as possible (for example, 1MB or more) This minimizes the blocking time in the kernel and may result in less time waiting for the disk (if the blocks being read happen to be continuous)

This is the code

ExecutorService exec = Executors.newFixedThreadPool(1);

// use RandomAccessFile because it supports readFully()
RandomAccessFile in = new RandomAccessFile("myfile.dat","r");
in.seek(0L);

while (in.getFilePointer() < in.length())
{
    int readSize = (int)Math.min(1000000,in.length() - in.getFilePointer());
    final byte[] data = new byte[readSize];
    in.readFully(data);
    exec.execute(new Runnable() 
    {
        public void run() 
        {
            // do something with data
        }
    });
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>