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.
source
share