Question

I would like to create a FileProcessor which will read the file sequentially. This class will be accessed by multiple threads and each thread will read the line and store it in data structure after processing the line. So far i have imagine the steps to solve the design problem as this:

  1. class FileProcessor with tasks: A method getline which will Open file. BufferedReader will read each line and return that line.

  2. 2nd class FileThread which implements runnable. In run method call getline() on FileProcessor object. this thread will also process the line and store it in data structure.

  3. 3rd Class will crate multiple threads of FileThreads.

I know this design has faults which i can imagine.

  1. How each thread will know if the EOF is reached so that they will stop.
  2. in the FileProcessor should i read the file normally with while loop detecting EOF or just read line by line and have threa take care of the EOF condition.

Please help.

Was it helpful?

Solution

No, the design isn't good.

The class that instantiates the Executor pool, reads the file, and doles out the work is the only one that needs to know about EOF. The threads that take their portion and do the work don't have to know.

Don't use raw Threads; leverage the new concurrency classes like Excecutor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top