Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java performance issue with Thread.sleep()

Tags:

Inline Java IDE hint states, "Invoking Thread.sleep in loop can cause performance problems." I can find no elucidation elsewhere in the docs re. this statement.

Why? How? What other method might there be to delay execution of a thread?

like image 768
fcw Avatar asked Oct 18 '10 03:10

fcw


People also ask

Is it good to 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 thread sleep is not recommended?

If given a wait of 5000 Milliseconds(5 seconds) and an element just take just 1-2 seconds to load, script will still wait for another 3 seconds which is bad as it is unnecessarily increasing the execution time. So thread. sleep() increases the execution time in cases where elements are loaded in no due time.

What can be used instead of thread sleep in Java?

For example when the threadA need to wait for a certain amount of time you can put the thread in wait state and start a timer task that after a certain amount of time (interval ) call notify and wake up the ThreadA that go ahead, this could be an alternative .

What happens when thread sleep () method is called?

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

It is not that Thread.sleep in a loop itself is a performance problem, but it is usually a hint that you are doing something wrong.

while(! goodToGoOnNow()) {    Thread.sleep(1000); } 

Use Thread.sleep only if you want to suspend your thread for a certain amount of time. Do not use it if you want to wait for a certain condition.

For this situation, you should use wait/notify instead or some of the constructs in the concurrency utils packages.

Polling with Thread.sleep should be used only when waiting for conditions external to the current JVM (for example waiting until another process has written a file).

like image 63
Thilo Avatar answered Sep 24 '22 18:09

Thilo


It depends on whether the wait is dependent on another thread completing work, in which case you should use guarded blocks, or high level concurrency classes introduced in Java 1.6. I recently had to fix some CircularByteBuffer code that used Thread sleeps instead of guarded blocks. With the previous method, there was no way to ensure proper concurrency. If you just want the thread to sleep as a game might, in the core game loop to pause execution for a certain amount of time so that over threads have good period in which to execute, Thread.sleep(..) is perfectly fine.

like image 26
Chris Dennett Avatar answered Sep 21 '22 18:09

Chris Dennett