Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Object.wait(millisec) to simulate sleep

Here's a snippet of code that I saw in some code I'm maintaining.

Object lock = new Object();

synchronized( lock ) 
{
   try
   {
       lock.wait( 50000 );
       Thread.sleep( 3000 );
   } 
   catch(Exception ex)
   {
   }
}

The developer wants to suspend the current thread for some amount of time and is using Object#wait as the mechanism. Obviously it is bad form to use the wait/notify protocol for this reason; however, is there any major difference between calling wait(millisec) and a Thread.sleep ?

like image 289
Jacques René Mesrine Avatar asked Apr 02 '09 04:04

Jacques René Mesrine


People also ask

How do I use wait instead of thread sleep?

Difference between wait() and sleep() The major difference is that wait() releases the lock while sleep() doesn't release any lock while waiting. wait() is used for inter-thread communication while sleep() is used to introduce a pause on execution, generally.

What is the use of wait () and sleep ()?

Wait() method releases lock during Synchronization. Sleep() method does not release the lock on object during Synchronization.

What is the main difference between wait () and sleep () method?

The sleep() method doesn't release any lock or monitor while waiting whereas wait() method releases the lock or monitor while waiting.

What is wait () sleep () Yield ()?

The main difference between wait and sleep is that wait() method releases the acquired monitor when the thread is waiting while Thread. sleep() method keeps the lock or monitor even if the thread is waiting.


2 Answers

Aside from having to get a monitor before waiting() there's no major difference anymore so long as no one external is going to be .notify()ing.

In ancient Java code you'd see people using wait() instead of Thread.sleep() because Thread.sleep() would freeze the whole application on systems without preemptive multitasking (I'm looking at you OS9). Technically wait() also let's you use nano-resolution waits, but in practice they're seldom that accurate.

like image 122
Ry4an Brase Avatar answered Oct 16 '22 19:10

Ry4an Brase


Note that there is one key difference in using Object.wait() and Thread.sleep() inside a synchronization block: Thread.sleep() does not release the locked monitor, so no-one else can become the owner of the monitor.

In addition, Object.wait() does not guarantee that the delay indicated will be obeyed strictly. First of all, after the delay passes, the thread may still be waiting for another thread that has become the monitor owner in the meantime; and it may compete with other threads waiting to grab the monitor.

Second, the mythical spurious wake-up, as it is described in the Java 6 API javadoc:

A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup.

Unlikely as it is, every piece of code using Object.wait() should take it into consideration.

like image 26
Bartosz Klimek Avatar answered Oct 16 '22 19:10

Bartosz Klimek