Is it always bad to use Thread.Sleep()?

后端 未结 5 1287
天命终不由人
天命终不由人 2021-01-01 08:59

I created an extension method for the the class Random which executes an Action (void delegate) at random times:

public static clas         


        
5条回答
  •  清酒与你
    2021-01-01 09:38

    Sleep "talks" to the Operation System that you want to suspend the thread. It's resource-intensive operation cause your thread uses RAM anyway (although it doesn't require processing time).

    With thread pool, you could use thread's resources (f.e. RAM) to process some other small tasks. To do it, the Windows lets you put a thread to sleep in a special alertable state, so it may be awakened and used temporarily.

    So Task.Delay let you put threads to alertable sleep, and therefore let you use resources of these thread unitl you don't need them.

提交回复
热议问题