Thread.Sleep(0) doesn't work as described?

前端 未结 5 1972
清酒与你
清酒与你 2021-01-17 19:49

I am currently reading this excellent article on threading and read the following text:

Thread.Sleep(0) relinquishes the thread’s current time slice i

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-17 20:21

    If you'd expand the width of the console to be 5 time larger than current then you'd see what you expect, lines not reaching the console width. The problem is one time slice is actually very long. So, to have the expected effect with normal console with you'd have to slow down the Points thread, but without using Sleep. Instead of while (true) loop try this

    for (int i = 0;; i++)
    {
      if (int % 10 == 0)
        s += ".";
    }
    

    To slow down the thread even more replace number 10 with bigger number.

提交回复
热议问题