Difference between wait() and sleep()

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

    Difference between wait() and sleep()

    • The fundamental difference is that wait() is from Object and sleep() is a static method of Thread.

    • The major difference is that wait() releases the lock while sleep() doesn’t release any lock while waiting.

    • wait() is used for inter-thread communication while sleep() is used to introduce a pause on execution, generally.

    • wait() should be called from inside synchronise or else we get an IllegalMonitorStateException, while sleep() can be called anywhere.

    • To start a thread again from wait(), you have to call notify() or notifyAll(). As for sleep(), the thread gets started after a specified time interval.

    Similarities

    • Both make the current thread go into the Not Runnable state.
    • Both are native methods.

提交回复
热议问题