Access File through multiple threads

后端 未结 10 805
天涯浪人
天涯浪人 2021-01-31 10:52

I want to access a large file (file size may vary from 30 MB to 1 GB) through 10 threads and then process each line in the file and write them to another file through 10 threads

10条回答
  •  佛祖请我去吃肉
    2021-01-31 11:33

    I would start with three threads.

    1. a reader thread that reads the data, breaks it into "lines" and puts them in a bounded blocking queue (Q1),
    2. a processing thread that reads from Q1, does the processing and puts them in a second bounded blocking queue (Q2), and
    3. a writer thread that reads from Q2 and writes to disk.

    Of course, I would also ensure that the output file is on a physically different disk than the input file.

    If processing tends to be faster slower than the I/O (monitor the queue sizes), you could then start experimenting with two or more parallell "processors" that are synchronized in how they read and write their data.

提交回复
热议问题