Thread.sleep and object.wait

This question already has an answer here:

  • Difference between wait() and sleep() 33 answers

  • One is used to synchronize Threads together while the other one is used to sleep for a given amount of time.

    If you want to synchronize Threads together, user wait/notify. If you want to sleep for a known amount of time, use Thread.sleep.


    No, Object.wait() will only ever cause the current thread to block, too.

    The main difference is that sleep() instructs the current thread to sleep for a period of time, whereas wait() instructs the current thread to release a monitor, then sleep until the monitor is notified. In other words, wait() is a coordination primitive between threads, whereas sleep() only cares about the passage of time (assuming no interruptions).


    Sleep and Wait looks deciving, They differ by a lot :

    Sleep - makes the Thread sleep for a given amount of time - good for Schedualing tasks, Animations and more...

    Wait - mostly used without limit of time, makes one thread Wait for something to heppen, this is the best practice for synchronization.

    if youre trying to Implement Wait by using Sleep, thats bad practice, which somewhat close to some very bad thing called Busy Waiting.

    链接地址: http://www.djcxy.com/p/92134.html

    上一篇: 我可以用等待而不是睡觉吗?

    下一篇: Thread.sleep和object.wait