Read large txt file multithreaded?

前端 未结 6 1837
忘了有多久
忘了有多久 2020-11-30 02:17

I have large txt file with 100000 lines. I need to start n-count of threads and give every thread unique line from this file.

What is the best way to do this? I thin

6条回答
  •  孤城傲影
    2020-11-30 02:38

    You can use the File.ReadLines Method to read the file line-by-line without loading the whole file into memory at once, and the Parallel.ForEach Method to process the lines in multiple threads in parallel:

    Parallel.ForEach(File.ReadLines("file.txt"), (line, _, lineNumber) =>
    {
        // your code here
    });
    

提交回复
热议问题