为什么这么Thread.sleep代码有害 [英] Why is Thread.Sleep so harmful

查看:258
本文介绍了为什么这么Thread.sleep代码有害的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我常常看到它提到, Thread.sleep代码(); 不应该被使用,但我不明白为什么会这样。如果 Thread.sleep代码(); 可引起麻烦,是否有同样的结果,这将是安全的替代解决方案。

如:

 ,而(真)
{
    做一些工作();
    我++;
    Thread.sleep代码(5000);
}

另外一个是:

 ,而(真)
{
    字符串[] =影像Directory.GetFiles(@C:\\目录,*。PNG);    的foreach(在图像串图片)
    {
        this.Invoke(()=> this.Enabled = TRUE);
        pictureBox1.Image =新位图(图片);
        Thread.sleep代码(1000);
    }
}


解决方案

与调用问题 Thread.sleep代码是<一个href=\"http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx\">explained相当简洁这里:


  

Thread.sleep代码有它的用途:模拟长时间的操作,而在MTA线程测试/调试。在.NET中有没有其他原因使用它。


  
  

Thread.sleep代码(N)表示阻塞当前线程,至少在数
  可内发生的时间片(或线程量程)的 N
  毫秒。
  一时间片的长度是不同的版本/类型的不同
  窗户和不同的处理器和通常为15至30
  毫秒。这意味着线程几乎肯定会阻止
  超过 N 毫秒。的可能性,你的线程会
  重新唤醒后, N 毫秒正好大约是不可能的,因为
  不可能的可能。 因此, Thread.sleep代码是毫无意义的计时


  
  

线程是一种有限的资源,他们采取约20万次
  创造约10万次破坏。默认情况下,他们
  保留1兆字节的虚拟内存为它的堆栈和使用2,000-8,000
  周期为每个上下文切换。 这使得任何等待线程
  的巨大的浪费。


在preferred解决办法:<一href=\"http://www.albahari.com/threading/part2.aspx#_Signaling_with_Event_Wait_Handles\">WaitHandles

最制成,错误是使用 Thread.sleep代码用whil​​e-结构(的演示,并回答,<一个href=\"http://www.zvolkov.com/clog/2009/07/10/net-4-0-and-better-ways-to-wait-for-queued-threads-to-complete/\">nice博客入境)

编辑:的结果
我想提高我的回答是:


  

我们有2个不同的使用情况:


  
  

      
  1. 我们正在等待,因为我们知道一个
      具体的时间跨度,当我们要继续(使用 Thread.sleep代码 System.Threading.Timer 或相似者)


  2.   
  3. 我们正在等待,因为有些病情变化一些时间...
      关键字(s)是/是的一段时间!如果条件检查在我们的code域,我们
      应该使用WaitHandles - 否则,外部组件应
      提供某种挂钩......如果它没有它的设计是坏的!


  4.   

  
  

我的答案主要包括用例2


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 there any alternative solutions with the same result that would be safe?

eg.

while(true)
{
    doSomework();
    i++;
    Thread.Sleep(5000);
}

another one is:

while (true)
{
    string[] images = Directory.GetFiles(@"C:\Dir", "*.png");

    foreach (string image in images)
    {
        this.Invoke(() => this.Enabled = true);
        pictureBox1.Image = new Bitmap(image);
        Thread.Sleep(1000);
    }
}

解决方案

The problems with calling Thread.Sleep are explained quite succinctly here:

Thread.Sleep has its use: simulating lengthy operations while testing/debugging on an MTA thread. In .NET there's no other reason to use it.

Thread.Sleep(n) means block the current thread for at least the number of timeslices (or thread quantums) that can occur within n milliseconds. The length of a timeslice is different on different versions/types of Windows and different processors and generally ranges from 15 to 30 milliseconds. This means the thread is almost guaranteed to block for more than n milliseconds. The likelihood that your thread will re-awaken exactly after n milliseconds is about as impossible as impossible can be. So, Thread.Sleep is pointless for timing.

Threads are a limited resource, they take approximately 200,000 cycles to create and about 100,000 cycles to destroy. By default they reserve 1 megabyte of virtual memory for its stack and use 2,000-8,000 cycles for each context switch. This makes any waiting thread a huge waste.

The preferred solution: WaitHandles

The most-made-mistake is using Thread.Sleep with a while-construct (demo and answer, nice blog-entry)

EDIT:
I would like to enhance my answer:

We have 2 different use-cases:

  1. We are waiting because we know a specific timespan when we should continue (use Thread.Sleep, System.Threading.Timer or alikes)

  2. We are waiting because some condition changes some time ... keyword(s) is/are some time! if the condition-check is in our code-domain, we should use WaitHandles - otherwise the external component should provide some kind of hooks ... if it doesn't its design is bad!

My answer mainly covers use-case 2

这篇关于为什么这么Thread.sleep代码有害的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆