How effective is Thread.sleep (long)?

Let's say I want to β€œpause” a thread so that other threads can work more efficiently. What is the minimum sleep period before blocking, becomes meaningless (or almost meaningless)?

+3
source share
3 answers

I would expect that any amount of sleep will at least be the functional equivalent of a call yield(), so there is never a point where it is "pointless". There is definitely a point where small values ​​are substantially indistinguishable, because the overhead of waiting for the operating system to return to your thread is more than a short sleep time. This probably happens somewhere in the range of 5-10, but, of course, it will be OS-specific.

+2
source

This is difficult to generalize and will depend on your specific problem. But I would not sleep on my own to control the flows. You can put threads in Executorand let them control them. You can also use PriorityQueue to accomplish your tasks.

+2

IIRC Thread.sleep(0) , , .

... , Thread.sleep .

, , , , . , , , Thread.sleep, .

+2

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


All Articles