Difference between sleep() and wait()?

sleep() is a method which is used to hold the process for few seconds or the time you wanted but in case of wait() method thread goes in waiting state and it won’t come back automatically until we call the notify() or notifyAll().

Ads by Google

The major difference is that wait() releases the lock or monitor while sleep() doesn’t releases any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution, generally.

Thread.sleep() sends the current thread into the “Not Runnable” state for some amount of time. The thread keeps the monitors it has acquired — i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread. Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.

object.wait() sends the current thread into the “Not Runnable” state, like sleep(), but with a twist. Wait is called on an object, not a thread; we call this object the “lock object.” Before lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the “wait list” associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original, waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.

synchronized(LOCK) {   
    Thread.sleep(1000); // LOCK is held
}

synchronized(LOCK) {   
    LOCK.wait(); // LOCK is not held
}

Let categorize all above points :

Call on:

  •     wait(): Call on an object; current thread must synchronize on the lock object.
  •     sleep(): Call on a Thread; always currently executing thread.

Synchronized:

  •     wait(): when synchronized multiple threads access same Object one by one.
  •     sleep(): when synchronized multiple threads wait for sleep over of sleeping thread.

Hold lock:

  •     wait(): release the lock for other objects to have chance to execute.
  •     sleep(): keep lock for at least t times if timeout specified or somebody interrupt.

Wake-up condition:

  •     wait(): until call notify(), notifyAll() from object
  •     sleep(): until at least time expire or call interrupt().

Usage:

  •     sleep(): for time-synchronization and;
  •     wait(): for multi-thread-synchronization.

 

Hope above information will add some value in your knowledge-base.

Happy Learning !!

Ads by Google

18 thoughts on “Difference between sleep() and wait()?”

  1. you write in easy way…really you are doing good job ..james gosling invented java and lokesh is making easyjava :)

    1. Puneet, because it was designed in this way only. These methods were introduced as means of making safe interactions between threads. If they were allowed without synchronization, there were occur lots of other problems such as race condition, data corruption and even program crash.

      Please note that a program does not get any kind of surety from low level OS thread scheduling, so it depends on language runtime to manage its thread.

      1. Thanks for the answer Lokesh..
        I have 1 more question!!!
        why wait(), notift(),notifyAll() are the methods of Object class, insteed of Thread Class?

          1. It is so because, Thread 2 will not have the idea of Thread 1 state, where t1 is in wait state or it is notified. That’s why there methods are of Object class. please let me know, if i am wrong.

  2. Lokesh, What happens if someone dosent call notify( earlier thread called wait) ?

    will this keep the program in hanged state? will it not allow program to exit/terminate?

    1. Tricky question.. In my view, no major impact on application just because one of several hundreds thread is blocked somewhere. Rest of application will run fine. This is very much clear and easy.

      No, program will not hang. According to theory, it should not allow to terminate and you might need to do it forcefully.

  3. Good points you are covering here, it will be more helpful if you take up the relatively new topics like concurrent package, annotations, generics, nio etc

Note:- In comment box, please put your code inside [java] ... [/java] OR [xml] ... [/xml] tags otherwise it may not appear as intended.

Leave a Reply

Your email address will not be published. Required fields are marked *


− five = 1

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>