I have a data file with thousand of rows. I am reading them and saving them in the database. I want to multi-thread this process in the batches of say 50 rows. As I am read in the file, 10 rows are submitted to an ExecutorService.
ExecutorService executor = Executors.newFixedThreadPool(5);`
I can do below in a in a while loop till my rows end....
Future<Integer> future = executor.submit(callableObjectThatSaves10RowsAtOneTime);
But, I don't want to read the entire file into memory if the processing 10 rows is taking time. I only want to submit 5 till one of the threads return, then I submit the next.
Let's say a thread takes 20 seconds to save the 10 records, I don't want the ExecutorService
to be fed thousand of lines since the reading process is continuing to read and submit to ExecutorService
What is the best way to achieve this?