javamultithreadingios-multithreading

Using more Threads for reading the same file in Java


How can I read a file in Java using multithreading? It doesn't matter if it's slower than using once, I have to do it. So, for example, if there are 2 threads, the first reads the first line and, at the same time, the second reads the second line; then the first reads the third line and the second reads the fourth line and they continue reading in this way since the end of the file. How can I implement this in Java?


Solution

  • Just use a single BufferedReader that is shared between the threads, and synchronize on it when calling readLine().

    It is completely pointless.