Difference between wait() and sleep()

前端 未结 30 3160
無奈伤痛
無奈伤痛 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:45

    Wait and sleep are two different things:

    • In sleep() the thread stops working for the specified duration.
    • In wait() the thread stops working until the object being waited-on is notified, generally by other threads.
    0 讨论(0)
  • 2020-11-22 00:45

    Wait() and sleep() Differences?

    Thread.sleep() Once its work completed then only its release the lock to everyone. until its never release the lock to anyone.

      Sleep() take the key, its never release the key to anyone, when its work completed then only its release then only take the key waiting stage threads.
    

    Object.wait() When its going to waiting stage, its will be release the key and its waiting for some of the seconds based on the parameter.

    For Example:

    you are take the coffee in yours right hand, you can take another anyone of the same hand, when will your put down then only take another object same type here. also. this is sleep() you sleep time you didn't any work, you are doing only sleeping.. same here also.

    wait(). when you are put down and take another one mean while you are waiting , that's wait

    you are play movie or anything in yours system same as player you can't play more than one at a time right, thats its here, when you close and choose another anyone movie or song mean while is called wait

    0 讨论(0)
  • 2020-11-22 00:45

    sleep() method causes the current thread to move from running state to block state for a specified time. If the current thread has the lock of any object then it keeps holding it, which means that other threads cannot execute any synchronized method in that class object.

    wait() method causes the current thread to go into block state either for a specified time or until notify, but in this case the thread releases the lock of the object (which means that other threads can execute any synchronized methods of the calling object.

    0 讨论(0)
  • 2020-11-22 00:46

    One key difference not yet mentioned is that while sleeping a Thread does not release the locks it holds, while waiting releases the lock on the object that wait() is called on.

    synchronized(LOCK) {
        Thread.sleep(1000); // LOCK is held
    }
    
    
    synchronized(LOCK) {
        LOCK.wait(); // LOCK is not held
    }
    
    0 讨论(0)
  • 2020-11-22 00:47
    1. wait() is a method of Object class.
      sleep() is a method of Thread class.

    2. sleep() allows the thread to go to sleep state for x milliseconds.
      When a thread goes into sleep state it doesn’t release the lock.

    3. wait() allows thread to release the lock and goes to suspended state.
      This thread will be active when a notify() or notifAll() method is called for the same object.

    0 讨论(0)
  • 2020-11-22 00:48

    Here, I have listed few important differences between wait() and sleep() methods.
    PS: Also click on the links to see library code (internal working, just play around a bit for better understanding).

    wait()

    1. wait() method releases the lock.
    2. wait() is the method of Object class.
    3. wait() is the non-static method - public final void wait() throws InterruptedException { //...}
    4. wait() should be notified by notify() or notifyAll() methods.
    5. wait() method needs to be called from a loop in order to deal with false alarm.

    6. wait() method must be called from synchronized context (i.e. synchronized method or block), otherwise it will throw IllegalMonitorStateException

    sleep()

    1. sleep() method doesn't release the lock.
    2. sleep() is the method of java.lang.Thread class.
    3. sleep() is the static method - public static void sleep(long millis, int nanos) throws InterruptedException { //... }
    4. after the specified amount of time, sleep() is completed.
    5. sleep() better not to call from loop(i.e. see code below).
    6. sleep() may be called from anywhere. there is no specific requirement.

    Ref: Difference between Wait and Sleep

    Code snippet for calling wait and sleep method

    synchronized(monitor){
        while(condition == true){ 
            monitor.wait()  //releases monitor lock
        }
    
        Thread.sleep(100); //puts current thread on Sleep    
    }
    

    0 讨论(0)
提交回复
热议问题