Reading a file by multiple threads

I have a 250 MB file to read. And the application is multithreaded. If I allow all threads to read a file, memory hunger occurs. I'm out of memory.

To avoid this. I want to have only one copy of the string (which is read from the stream) in memory, and I want all the threads to use it.

while (true) {
    synchronized (buffer) {
        num = is.read(buffer);
            String str = new String(buffer, 0, num);

    }
    sendToPC(str);
}

Basically, I want to have only one copy of the line, when all the threads are complete, I want to read the second line, etc.

+1
source share
4 answers

? , . . , , .

, ?

+6

, , , FileChannel. , , , ( MappedByteBuffer.load()) , , .

. javadoc FileChannel, RandomAccessFile MappedByteBuffer

+2

, ?

+1

You can register all threads as callbacks in the file reader class. SOs have something like an array or a list of classes that implement the StringReaderThread interface, which has a processString (line input) method. After reading each line from the file, iterate over this / list array and call processString () for all threads this way. Will this solve your problem?

+1
source

All Articles