Difference between wait() and sleep()

前端 未结 30 3136
無奈伤痛
無奈伤痛 2020-11-22 00:24

What is the difference between a wait() and sleep() in Threads?

Is my understanding that a wait()-ing Thread is still in runni

30条回答
  •  天涯浪人
    2020-11-22 00:27

    Example about sleep doesn’t release lock and wait does

    Here there are two classes :

    1. Main : Contains main method and two threads.
    2. Singleton : This is singleton class with two static methods getInstance() and getInstance(boolean isWait).

      public class Main {
      
      private static Singleton singletonA = null;
      private static Singleton singletonB = null;
      
      public static void main(String[] args) throws InterruptedException {
      
      Thread threadA = new Thread() {
          @Override
          public void run() {
      
              singletonA = Singleton.getInstance(true);
      
          }
      };
      
      Thread threadB = new Thread() {
          @Override
          public void run() {
              singletonB = Singleton.getInstance();
      
              while (singletonA == null) {
                  System.out.println("SingletonA still null");
              }
      
              if (singletonA == singletonB) {
                  System.out.println("Both singleton are same");
              } else {
                  System.out.println("Both singleton are not same");
              }
      
          }
      };
      
      threadA.start();
      threadB.start();
      
       }
      }
      

    and

    public class Singleton {
    
        private static Singleton _instance;
    
        public static Singleton getInstance() {
    
        if (_instance == null) {
            synchronized (Singleton.class) {
                if (_instance == null)
                    _instance = new Singleton();
            }
        }
        return _instance;
    
    }
    
    public static Singleton getInstance(boolean isWait) {
    
        if (_instance == null) {
            synchronized (Singleton.class) {
                if (_instance == null) {
                    if (isWait) {
                        try {
                            // Singleton.class.wait(500);//Using wait
                            Thread.sleep(500);// Using Sleep
                            System.out.println("_instance :"
                                    + String.valueOf(_instance));
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
    
                    _instance = new Singleton();
                }
            }
        }
        return _instance;
    
     }
    }
    

    Now run this example you will get below output :

    _instance :null
    Both singleton are same
    

    Here Singleton instances created by threadA and threadB are same. It means threadB is waiting outside until threadA release it’s lock.

    Now change the Singleton.java by commenting Thread.sleep(500); method and uncommenting Singleton.class.wait(500); . Here because of Singleton.class.wait(500); method threadA will release all acquire locks and moves into the “Non Runnable” state, threadB will get change to enter in synchronized block.

    Now run again :

    SingletonA still null
    SingletonA still null
    SingletonA still null
    _instance :com.omt.sleepwait.Singleton@10c042ab
    SingletonA still null
    SingletonA still null
    SingletonA still null
    Both singleton are not same
    

    Here Singleton instances created by threadA and threadB are NOT same because of threadB got change to enter in synchronised block and after 500 milliseconds threadA started from it’s last position and created one more Singleton object.

提交回复
热议问题