Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Acceptable use of Thread.Sleep()

Tags:

c#

sleep

I'm working on a console application which will be scheduled and run at set intervals, say every 30 minutes. Its only purpose is to query a Web Service to update a batch of database rows.

The Web Service API reccommends calling once every 30 seconds, and timeout after a set interval. The following pseudocode is given as an example:

listId := updateList(<list of terms>)
LOOP
  WHILE NOT isUpdatingComplete(listId)
END LOOP
statuses := getStatuses(“LIST_ID = {listId}”)

I have coded this roughly in C# as:

int callCount = 0;
while( callCount < 5 && !client.isUpdateComplete(listId, out messages) )
{
    listId = client.updateList(options, terms, out messages);
    callCount++;
    Thread.Sleep(30000);
}
// Get resulting status...

Is it OK in this situation to use Thread.Sleep()? I'm aware it is not generally good practice but from reading reasons not to use it this seems like acceptable usage.

Thanks.

like image 338
TonE Avatar asked Jan 10 '12 15:01

TonE


People also ask

Is it good to use thread sleep?

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

Should we use thread sleep in Java?

Java Thread Sleep important pointsIt always pause the current thread execution. The actual time thread sleeps before waking up and start execution depends on system timers and schedulers. For a quiet system, the actual time for sleep is near to the specified sleep time but for a busy system it will be little bit more.

Why we will not use thread sleep method?

The reason people discourage Thread. sleep is because it's frequently used in an ill attempt to fix a race condition, used where notification based synchronization is a much better choice etc. In this case, AFAIK you don't have an option but poll because the API doesn't provide you with notifications.

How long should thread sleep?

If it is a UI worker thread, as long as they have some kind of progress indicator, anywhere up to half a second should be good enough. The UI should be responsive during the operation since its a background thread and you definitely have enough CPU time available to check every 500 ms.


2 Answers

Thread.Sleep ensures the current thread doesn't return until at least the specified milliseconds have passed. There are plenty of places it's appropriate to do that, and your example seems fine, assuming it's running on a background thread.

Some example places you don't want to use it - on the UI thread or where you need to do exact timing.

like image 87
Kieren Johnstone Avatar answered Sep 24 '22 11:09

Kieren Johnstone


Generally speaking, Thread.Sleep is like any other tool: perfectly OK to use, except when it's terribly misused. I disagree with the "not generally good practice" part, which is the result of people abusing Thread.Sleep when they should be doing something else (i.e. blocking on a synchronization object).

In your case the program is single-threaded, it has no UI (i.e. the thread has no message loop) and you do not want to synchronize with external events. Therefore Thread.Sleep is just fine.

like image 35
Jon Avatar answered Sep 21 '22 11:09

Jon