Why is my multi-threading slower than my single threading?

后端 未结 6 1688

I know a couple of people asked a question similar to this, but I can’t find any response that would make me understand why it\'s slower.

So, I made a little console

6条回答
  •  一整个雨季
    2020-12-14 07:00

    From the official Console documentation

    I/O operations that use these streams are synchronized, which means that multiple threads can read from, or write to, the streams. This means that methods that are ordinarily asynchronous, such as TextReader.ReadLineAsync, execute synchronously if the object represents a console stream

    This means that the console class handles the thread synchronization so if thread A and thread B are trying to write to the console the console will handle them and only one for time will be able to write. The handling logic behind it is the reason why it takes longer


    UPDATE
    I suggest you to have a look at Parallel.ForEach

提交回复
热议问题