Read large txt file multithreaded?

前端 未结 6 1838
忘了有多久
忘了有多久 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:49

    Something like:

    public class ParallelReadExample
    {
        public static IEnumerable LineGenerator(StreamReader sr)
        {
            while ((line = sr.ReadLine()) != null)
            {
                yield return line;
            }
        }
    
        static void Main()
        {
            // Display powers of 2 up to the exponent 8:
            StreamReader sr = new StreamReader("yourfile.txt")
    
            Parallel.ForEach(LineGenerator(sr), currentLine =>
                {
                    // Do your thing with currentLine here...
                } //close lambda expression
            );
    
            sr.Close();
        }
    }
    

    Think it would work. (No C# compiler/IDE here)

提交回复
热议问题