Why is Thread.Sleep so harmful

前端 未结 8 2027
天涯浪人
天涯浪人 2020-11-22 06:30

I often see it mentioned that Thread.Sleep(); should not be used, but I can\'t understand why this is so. If Thread.Sleep(); can cause trouble, are

8条回答
  •  无人及你
    2020-11-22 06:40

    I have a use case that I don't quite see covered here, and will argue that this is a valid reason to use Thread.Sleep():

    In a console application running cleanup jobs, I need to make a large amount of fairly expensive database calls, to a DB shared by thousands of concurrent users. In order to not hammer the DB and exclude others for hours, I'll need a pause between calls, in the order of 100 ms. This is not related to timing, just to yielding access to the DB for other threads.

    Spending 2000-8000 cycles on context switching between calls that may take 500 ms to execute is benign, as does having 1 MB of stack for the thread, which runs as a single instance on a server.

提交回复
热议问题