Difference between wait() and sleep()

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

    One potential big difference between sleep/interrupt and wait/notify is that

    • calling interrupt() during sleep() always throws an exception (e.g. InterruptedException), whereas
    • calling notify() during wait() does not.

    Generating an exception when not needed is inefficient. If you have threads communicating with each other at a high rate, then it would be generating a lot of exceptions if you were calling interrupt all the time, which is a total waste of CPU.

提交回复
热议问题