How long should the thread sleep when the event pattern is not applied?

In the main workflow cycle, which I have to interrogate in order to know if it works, I let it sleep so as not to wait. How to determine a good length for sleep? E. g. if I wake up every millisecond, it is probably useless to sleep at all. If I wake up every ten minutes, this may make the application look unresponsive (depending on what the thread is doing).

I heard so far that the frame says below that the user user will not notice the time frame of about 100 ms, but what about approaching it from the machine? How small can an interval go before it starts wastefully?

I think it comes down to a more general (e.g. platform independent) version of this question .

EDIT: Of course, the question should always be rephrased first like this: "How can I change this to an event template instead of polling," but let's say that now I cannot or do not want.

+3
source share
5 answers

Sometimes a survey is the answer .

However, how often it depends on what the thread does, so make it customizable.

, . , ( ). - , , 30 , , . MOMENT ( 5 ).

, / , . 1-5 ( , ).

, , , . , Windows, , 60 , 60 .

, Rip Van Winkle .

, , , .

,

+7

? , ? , :

while(!workComplete) {
  Thread.Sleep(n);
}

, - , . , 500 . , , , . , , .

. , 200 , . , 400 . , 800 . .. , TimeoutException, .

+2

, . , Java wait/notify.

, , 100 . .

+1

You must give a signal to warn the flow. If you use c, you can use something like a conditional variable from p-streams. If you are using something newer, such as .NET, you can use EventWaitHandle. I am sure that java and other 4gl have similar classes like EventWaitHandle.

0
source

Let him sleep forever, awakening him when there is an entrance available?

-1
source

Source: https://habr.com/ru/post/1703751/


All Articles