Does Thread.Sleep affect the ThreadPool?

拜拜、爱过 提交于 2019-12-04 14:51:48

Assuming that the thread you put on sleep was obtained from thread pool then surely it does affect the thread pool. If you explicitly say that the thread should sleep then it cannot be reused by the thread pool during this time. This may cause the thread pool to spawn new threads if there are some jobs awaiting to be scheduled. Creating a new thread is always expensive - threads are system resources.

You can however look at Task.Delay method (along with async and await) that suspends executing code in a more intelligent way - allowing the thread to be reused during waiting. Refer to this Thread.Sleep vs. Task.Delay article.

Thread.Sleep() affects the thread it's called from, if you're calling Thread.Sleep() in a ThreadPool thread and trying to queue up more it may be hitting the max count of ThreadPool threads and waiting for a thread to finish before executing another.

http://msdn.microsoft.com/en-us/library/system.threading.threadpool.setmaxthreads.aspx

Sebastian Job Bjørnager Jensen

No, the Thread.Sleep() is only on the current thread. Thread.Sleep(int32) documentation:

The number of milliseconds for which the thread is suspended.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!