Python微信订餐小程序课程视频
https://edu.csdn.net/course/detail/36074
Python实战量化交易理财系统
https://edu.csdn.net/course/detail/35475
sleep 方法和 wait 方法都是用来将线程进入休眠状态的,并且 sleep 和 wait 方法都可以响应 interrupt 中断,也就是线程在休眠的过程中,如果收到中断信号,都可以进行响应,并抛出 InterruptedException 异常。那 sleep 和 wait 的区别都有哪些呢?接下来,我们一起来看。
区别一:语法使用不同
wait 方法必须配合 synchronized 一起使用,不然在运行时就会抛出 IllegalMonitorStateException 的异常,如下代码所示:
![image.png](https://img-blog.csdnimg.cn/img_convert/8e97bb251e7b43fb2e73527567e255e1.png#clientId=u1db76bc1-8d85-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=708&id=u23689d57&margin=[object Object]&name=image.png&originHeight=1415&originWidth=1748&originalType=binary&ratio=1&rotation=0&showTitle=false&size=215876&status=done&style=none&taskId=u2ee6286c-2933-4470-8aee-8cc656d2de4&title=&width=874)
初看代码好像没啥问题,编译器也没报错,然而当我们运行以上程序时就会发生如下错误:
![image.png](https://img-blog.csdnimg.cn/img_convert/f092037465857e343edc05ec3de45900.png#clientId=u1db76bc1-8d85-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=475&id=u2fd52422&margin=[object Object]&name=image.png&originHeight=949&originWidth=2425&origina