Why is Thread.Sleep so harmful

前端 未结 8 2026
天涯浪人
天涯浪人 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:52

    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

    0 讨论(0)
  • 2020-11-22 06:53

    For those of you who hasn't seen one valid argument against use of Thread.Sleep in SCENARIO 2, there really is one - application exit be held up by the while loop (SCENARIO 1/3 is just plain stupid so not worthy of more mentioning)

    Many who pretend to be in-the-know, screaming Thread.Sleep is evil failed to mentioned a single valid reason for those of us who demanded a practical reason not to use it - but here it is, thanks to Pete - Thread.Sleep is Evil (can be easily avoided with a timer/handler)

        static void Main(string[] args)
        {
            Thread t = new Thread(new ThreadStart(ThreadFunc));
            t.Start();
    
            Console.WriteLine("Hit any key to exit.");
            Console.ReadLine();
    
            Console.WriteLine("App exiting");
            return;
        }
    
        static void ThreadFunc()
        {
            int i=0;
            try
            {
                while (true)
                {
                    Console.WriteLine(Thread.CurrentThread.ThreadState.ToString() + " " + i);
    
                    Thread.Sleep(1000 * 10);
                    i++;
                }
            }
            finally
            {
                Console.WriteLine("Exiting while loop");
            }
            return;
        }
    
    0 讨论(0)
提交回复
热议问题