Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does usage of Thread.Sleep(n) causes performance issues?

I am using Thread.Sleep(n) for my project. I heard that Thread.Sleep can cause performance issues but not sure about it.

My requirement is:

Wait for 5 minute increments, up to 30 minutes (6 times the 5 minute delay). After this, begin to increment by 1 hour and do this 5 times (additional 5 hours).

Below I have provided my sample code which uses Thread.Sleep(n) in different scenarios:

Thread.Sleep(1000 * 60 * 5); //-------waiting for 5 minutes
var isDownloaded = false;
try
{
    var attempt = 0;
    while (attempt < 11)
    {
        isDownloaded = TryDownloading(strPathToDownload, strFileToDownload);
        if (isDownloaded)
            break;
        attempt++;
        if (attempt < 6)
            Thread.Sleep(1000 * 60 * 5); //--------waiting for 5 minutes
        else
        {
            if (attempt < 11)
                Thread.Sleep(1000 * 60 * 60); //-------waiting for 1 hour
            else
                break;
        }
    }

}

On the above code I am trying to download a file with a maximum of 11 download attempts. Initially it waits for 5 minutes and then try to download the file which is the first attempt and if failed then it tries for the next 5 attempts with an interval of 5 minutes each. If they failed for the first six attempts then it go for the next 5 attempts with an interval for 1 hour each.

So we decided to use Thread.Sleep for those time delay in our console app.

Does this causes any problem or performance issues?

If Thread.Sleep(n) causes performance issues, then which would be an better alternative way instead of using Thread.Sleep(n)?

Also finally, Does MSDN suggested that Thread.Sleep(n) is harmful or it shouldn't be used?

like image 894
RajeshKannan Avatar asked Jan 23 '14 13:01

RajeshKannan


People also ask

Does thread sleep consume CPU?

Then you can be sure if the thread was actually sleeping or was doing something else. To be more clear: Threads consume no CPU at all while in not runnable state. Not even a tiny bit. This does not depend on implementation.

What is the disadvantage of thread sleep?

Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.

Is it a good practice to use thread sleep?

Using Thread. sleep() frequently in an automation framework is not a good practice.

What happens when thread sleeps?

Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.


2 Answers

This is absolutely fine. Here are the costs of sleeping:

  • You keep a thread occupied. This takes a little memory.
  • Setting up the wait inside of the OS. That is O(1). The duration of the sleep does not matter. It is a small, constant cost.

That's all.

What is bad, though, is busy waiting or doing polling loops because that causes actual CPU usage. Just spending time in a sleep or wait does not create CPU usage.

TL;DR: Use sleep for delays, do not use sleep for polling.

I must say that the aversion against sleeping is sometimes just a trained reflex. Be sure to analyze the concrete use case before you condemn sleeping.

like image 124
usr Avatar answered Nov 10 '22 01:11

usr


Do not use ASP.NET worker process to run long running tasks!

The app pool may be recycled any time and you will lose your sleeping threads.

Consider using Windows Service instead. You can communicate between web site and windows service using database or messaging.

like image 26
Jakub Konecki Avatar answered Nov 10 '22 01:11

Jakub Konecki