I have two threads that read two files. Each thread collects information from objects. As the two threads continue to read the file, I want to add the generated objects to a map or a temp list which are shared by the threads, and I want to process the added objects from the map/temp list simultaneously as they are added.
I am interested in using producer/consumer model for this. But will it be the way to use it on a process which itself depends on two threads? Can BlockingQueue
s be used in these cases?
Unless the files are too large to fit into memory, you would be better off reading all of file1 into memory, then reading all of file2 into memory, then processing them - ideally this will require only two disk seeks. If you try to read both files simultaneously then you're going to cause the disk head to do a lot of seeks between the two files.
If the files are too large to fit into memory, then read a chunk of File1, then read a chunk of File2, then process the chunks and null their references so the garbage collector can take care of them; repeat until the files have been completely processed.
Once the files are read into memory you can process them in parallel, and then a BlockingQueue
would be an appropriate data structure.