Thursday, 16 July 2015

Difference between sleep and wait - Java Multi-threading

Difference between sleep and wait is a popular Java multi-threading interview question. It is another confusing question just like Difference between yield and sleep because functionality wise both sleep() and wait() look quite similar.

When sleep method is called with a specified time currently executing thread goes in a TIMED_WAITING state.

Same way when wait method is called on an object the thread currently holding the lock on that object goes in a waiting state (or TIMED_WAITING state depending upon which of the three wait methods is being called).

  1. The very first difference is that sleep() is defined as a static method in Thread class and operates on the currently executing thread.
    Whereas wait() method is in Object class and operates on the thread which is currently holding the lock on the object on which the wait method is called.
  2. Since wait method is supposed to release the lock on an object so it has to be called from a synchronized context (with in a synchronized method or synchronized block). Not calling wait() method from a synchronized context will result in a IllegalMonitorStateException being thrown.
    With sleep method there is no such mandatory condition it doesn't need to be called from a synchronized context.
  3. wait() method releases the lock it holds on an object before going to waiting state which gives another thread a chance to enter the synchronized block.
    sleep() method, if called from a synchronized block or method, will not release the lock so another thread won't get a chance to enter the synchronized block.
  4. When wait() method is called upon an object the thread which is currently holding the lock on that object goes to waiting state and it changes state to runnable only when the notify() or notifyAll() method is called by some thread on the same object.

    There are overloaded wait() methods too

       public final void wait(long timeout) throws InterruptedException
       
    And
      public final void wait(long timeout, int nanos) throws InterruptedException
    

    which causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

    In case of sleep() method thread changes state to runnable after the specified time is elapsed. As example Thread.sleep(4000); will make the currently executing thread to sleep for 4 seconds and thread is ready to run again when the specified time has elapsed . This sleep period can be terminated by interrupts.

An example of interrupt

 public class InterruptDemo extends Thread {
    @Override
    public void run(){  
        try {
            Thread.sleep(4000);
        } catch (InterruptedException Iexp) {
            System.out.println("Thread Interrupted exception "+ Iexp); 
        }
         
        System.out.println("thread after interruption...");  
    }  
    public static void main(String[] args) {        
        InterruptDemo thread1 = new InterruptDemo();
        thread1.start();
        thread1.interrupt();
    }
}

Output

Thread Interrupted exception java.lang.InterruptedException: sleep interrupted
thread after interruption...

That's all for this topic Difference between sleep and wait. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Can we start the same thread twice in Java
  2. What if run() method called directly instead of start() method
  3. Synchronization in Java multithreading
  4. Race condition in Java multi-threading
  5. Deadlock in Java multi-threading
  6. Java Multi-threading interview questions

You may also like -

No comments:

Post a Comment