Monday 14 September 2015

Difference between sleep() and wait()

sleep() method:
This method can be used for time-synchronization i.e. sleep() method is used to introduce pause on execution.

sleep() is a method which is used to hold the process for few seconds or the time.

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 currentThread.interrupt() it will wake up the sleeping thread.

wait() method:
This method can be used for multithread-synchronization i.e. wait() method is used for inter-thread communication.

wait() method thread goes in waiting state and it won’t come back automatically until we call the notify() or notifyAll().

wait() is called on an object called as “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
       }

wait()
sleep()
Call on
wait() is an instance method of Object class and Call on an object; current thread must synchronize on the lock object.
Thread.sleep() method is a static method and applies on currently executing thread.
Synchronized
When synchronized multiple threads access same Object one by one.
When synchronized multiple threads wait for sleep over of sleeping thread.
Hold lock
Release the lock for other Threads to have chance to execute.
Keep lock for at least for specified time, if timeout specified or somebody interrupts.
Sleep/Wake-up condition
until call notify(), notifyAll() from object
Until at least time expire or call interrupt().
Usage
Inter-thread communication use wait() method.
For time-synchronization:
To pause a thread execution, use sleep() method.
Runnable state
Sleeping thread immediately goes to Runnable state after waking up.
Waiting thread first acquires the lock and then goes into Runnable state.
synchronized method/block
wait() method is always must be called from synchronized method/ block.
sleep() method can be called without synchronized method/ block.

Thread.sleep() Example

public class SleepDemo implements Runnable {

       @Override
       public void run() {
              for (int i = 1; i <= 3; i++) {

                 System.out.println(Thread.currentThread().getName()+"  "+i);
                 try {
                       // thread to sleep for 3000 milliseconds
                       Thread.sleep(3000);
                 } catch (Exception e) {
                       System.out.println(e);
                 }
              }
       }

       public static void main(String[] argsthrows Exception {
            Thread thread1 = new Thread(new SleepDemo());
            thread1.start();

            Thread thread2 = new Thread(new SleepDemo());
            thread2.start();
       }
}
Output:
Thread-0  1
Thread-1  1
Thread-0  2
Thread-1  2
Thread-0  3
Thread-1  3

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...