Tuesday 2 April 2013

Difference between Wait and Sleep in Java

Leave a Comment



  1. Main difference between wait and sleep is that wait() method release the acquired lock when thread is waiting while Thread.sleep() method keeps the lock or monitor even if thread is waiting.
  2. wait method in java should be called from synchronized method or block while there is no such requirement for sleep() method.
  3. Thread.sleep() method is a static method and applies on current thread, while wait() is an instance specific method and only got wake up if some other thread calls notify method on same object.
  4. in case of sleep, sleeping thread immediately goes to Runnable state after waking up while in case of wait, waiting thread first acquires the lock and then goes into Runnable state.
  5. wait is called on Object while sleep is called on Thread.
  6. wait is called from synchronized context only while sleep can be called without synchronized block.
  7. Thread.sleep() method is a static method and always puts current thread on sleep.
So based upon your need if you require a specified second of pause use sleep() method or if you want to implement inter-thread communication use wait method.

/*
 * Example of Thread Sleep method in Java
 */
public class SleepTest {
      
       public static void main(String... args){
              System.out.println(Thread.currentThread().getName() + " is going to sleep for 1 Second");
              try {
                     Thread.currentThread().sleep(1000);
              } catch (InterruptedException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
              System.out.println("Main Thread is woken now");
       }

}

Output:
main is going to sleep for 1 Second
Main Thread is woken now

wait example
package defaultt;

public class Customer {
int amount=0;
int flag=0;
public synchronized int withdraw(int amount){
 System.out.println(Thread.currentThread().getName()+" is going to withdraw");
 
       if(flag==0){
        try{
  System.out.println("waiting....");
  wait();
 }catch(Exception e){}
 }
       
 this.amount-=amount;
 System.out.println("withdraw completed");
 return amount;
}

public synchronized void deposit(int amount){
 System.out.println(Thread.currentThread().getName()+" is going to  deposit");
 this.amount+=amount;
 
 notifyAll();
 System.out.println("deposit completed");
        flag=1;
 }


}


package defaultt;

public class Test{
public static void main(String[] args) {
 final Customer c=new Customer();
 
 Thread t1=new Thread(){
  public void run(){
   c.withdraw(5000);
   System.out.println("After withdraw amount is"+c.amount);
  }
 };
 
 Thread t2=new Thread(){
  public void run(){
   c.deposit(9000);
   System.out.println("After deposit amount is "+c.amount);
  }
 };
 
 
 t1.start();
 t2.start();
 
 
}
}

Output:
Thread-0 is going to withdraw
waiting....
Thread-1 is going to  deposit
deposit completed
withdraw completed
After deposit amount is 4000
After withdraw amount is4000

0 comments:

Post a Comment