Difference between wait() and sleep()

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

提交回复
热议问题