I have a text processing application in Java, which reads a file chunk by chunk (~100000 lines) and processes each chunk in a separate thread.
It works well, but there is an issue. Reading lines is much faster than processing them and program ends up with a queue of Runnables waiting for their turn. That costs some memory which I intend to save up.
I would like the program to behave that way:
- read 16 chunks and submit them to 8 runnables;
- if number of unprocessed chunks falls below 12 read 4 more chunks of text.
That will keep Runnables busy, but at the same time keep save memory for processing (instead of storing chunks).
How do I do it in Java? Written in preudocode I want this:
loop {
chunk = readChunkOfData();
counter.inc();
processAsync(chunk);
if (counter.isBiggerThan(16)) {
counter.sleepWhileCounterIsBiggerThan(12);
}
}
...
worker {
// do the job
counter.dec();
}