在多线程应用程序中使用Thread.Sleep的原因是什么?
很多时候,当我看到一些多线程代码时,我会在代码中看到 Thread.Sleep() 语句。
我什至在试图找出问题所在时发生了崩溃,因此注释掉了大部分多线程代码并慢慢地把它带来了,当我添加了一个 for 语句时,例如:
for ( int i = 0; i < 1000000; ++i )
++i;
它没有崩溃。所以现在我替换了它 Thread.Sleep() ,它似乎可以工作。我无法轻松复制它以将其发布到此处,但是对于多线程应用程序是否需要使用 Thread.Sleep() ?
他们的目的是什么?如果不使用会导致意想不到的结果吗?
编辑:顺便说一句,我正在使用 BackgroundWorker
并且只在那里实现我的东西,但不确定是什么原因导致的。虽然我使用的 API 是托管应用程序,但该应用程序不是多线程的。例如,我认为我不能同时在多个线程上调用它的 API 函数。不确定,但那是我的猜测。
Often times I when I see some multi-threaded code, I see Thread.Sleep()
statements in the code.
I even had a crash where I was trying to figure out the problem, so commented out most of the multi-threaded code and slowly brought it and for the final piece when I added a for statement like:
for ( int i = 0; i < 1000000; ++i )
++i;
it didn't crash. So now I replaced it Thread.Sleep()
and it seems to work. I can't repro it easily to post it here, but is using Thread.Sleep()
necessary for multi-threaded applications?
What's the purpose of them? Would it lead to unexpected results if not used?
EDIT: Btw I am using the BackgroundWorker
and only implementing my stuff in there, but not sure what causes this. Although I am using an API which is the hosting app where the app is not multi threaded. So for instance I think I can't call it's API functions on several threads at once. Not sure, but that was my guess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常,Thread.Sleep 是糟糕设计的标志。话虽这么说,它比占用 100% 的 CPU 核心时间要好得多,这就是上面的 for 循环所做的。
更好的选择通常是使用
WaitHandle
,例如ManualResetEvent
,在“事件”(这是延迟的原因)发生时触发线程的继续执行。发生。或者,在许多情况下使用Timer
也可以发挥作用。Typically,
Thread.Sleep
is a sign of a bad design. That being said, its MUCH better than eating 100% of the CPU core time, which is what the for loop above is doing.A better option is typically to use a
WaitHandle
, such as aManualResetEvent
, to trigger the continuation of the thread's execution when the "event" (which is the reason to delay) occurs. Alternatively, using aTimer
can work as well in many cases.Thread.Sleep(1)
允许切换到执行另一个线程。因此,如果您的线程多于核心/处理器,并且您知道“现在我在这个线程中做了很多工作,接下来的工作可以稍后完成”,您可以调用 Thread.Sleep(1) 并允许另一个线程比本机切换器“暂停”当前执行的线程更快地完成某些工作。The
Thread.Sleep(1)
allows switch to execution another thread. So if you have more threads than cores/processors and you know "now I did in this thread a lot of work and next work can be done little-bit later" you callThread.Sleep(1)
and allows another thread to do some work sooner than the native switcher will "pause" the currently executed thread.试试这个:编写一个启动 100 个线程的程序,并将每个线程放入
for
循环中,如您所描述的。然后编写另一个启动 100 个线程并使用 Thread.Sleep 来代替。运行它们并比较 CPU 使用率。你就会明白这一点。 =)
Try this: Write a program that launches 100 threads, and put each of the thread into a
for
loop as you described. And then write another that launches 100 threads and usesThread.Sleep
instead.Run them both and compare the CPU usage. You'll see the point. =)
Thread.Sleep()
只是导致执行线程暂停指定的持续时间。我见过许多开发人员使用 Thread.Sleep() ,因为他们可能不处理依赖线程的连接。他们只是使用 Thread.Sleep() 来强制线程等待一段时间,直到认为其他线程已经完成并获得可用的数据。
如果您有两个线程需要相互等待才能继续处理,那么您确实应该使用 .NET 内置的机制来处理此类情况(即
ManualResetEvent
等) .)Thread.Sleep()
simply causes the executing thread to halt for the specified duration.I've seen many developers use
Thread.Sleep()
because they don't probably handle the joining of dependent threads. They simply useThread.Sleep()
to force a thread to wait for some amount of time until the think their other threads would have finished and have their data available.If you have two threads that need to wait on each other to proceed with their processing, you should really use the mechanisms built in to .NET that are meant to handle situations like that (ie.
ManualResetEvent
, etc.)Thread.Sleep()
在某些情况下可以使用,例如。看门狗线程。然而,就您的情况而言,这似乎不是其他人指出的最佳解决方案。
没有代码示例,很难判断,但根据您的描述,我认为这不是 Thread.Sleep() 与否的问题。我怀疑您可能遇到了竞争状况 - 这通常就是您在多线程代码中经历“随机”错误行为甚至“随机”崩溃的原因 - 就像您所经历的那样。
无论出于何种原因,您的 for 循环可能会导致竞争条件的微妙关键时间发生频率降低,但它无法解决根本原因。在进行多线程编程时有很多陷阱需要注意,如果您想避免这些陷阱,我只能建议您阅读该主题。
我建议阅读 http://www.amazon.com/Concurrent-Programming-Windows -乔-达菲/dp/032143482X
Thread.Sleep()
is OK to use in some situations eg. watchdog threads.However in your case, it may not seem to be the optimal solution as pointed out by others.
Without a code sample, it's hard to tell, but based on your description, I don't think it's a question of
Thread.Sleep()
or not. I would suspect that you may be suffering from a race condition - that's usually why you experience "random" buggy behavior or even "random" crashes in multithreaded code - as seems to be what you are experiencing.For whatever reason, your for-loop may cause the subtle critical timings of the race condition to occur less often, but it won't solve the root cause. There are many pitfalls to be aware of when doing multithreaded programming, I can only advice you to read up on the topic if you want to be able to avoid these.
I'll recommend reading http://www.amazon.com/Concurrent-Programming-Windows-Joe-Duffy/dp/032143482X