java中的sleep和wait的区别
sleep和wait的区别: 1、sleep的意思是:睡,睡觉,睡眠。 2、wait的意思是:等候,推迟,延缓等待,耽搁,伺候用餐。 拓展资料 sleep的用法 1、They were exhausted from lack of sleep 由于缺乏睡眠,他们非常疲惫。 2、During the car journey, the baby slept 坐车来的路上,宝宝睡着了。 3、I think he may be ready for a sleep soon. 我想他也许很快就要睡一觉了。 4、I can't get to sleep with all that singing. 那些歌声搅得我无法入睡。 5、I didn't lose too much sleep over that investigation. 我并不太担心那个调查。 wait 1、I walk to a street corner and wait for the school bus 我走到街角等校车。 2、There'll be a car waiting for you 会有辆汽车等你。 3、I want to talk to you, but it can wait 我想和你谈谈,但可以晚点再说。 4、If you think this all sounds very exciting, just wait until you read the book 如果你觉得所有这些听起来令人兴奋,那就等着去读这本书吧。 5、'Wait a minute!' he broke in. 'This is not giving her a fair hearing!' “等一下,”他插嘴说,“这没有给她一个公平的解释机会!”
java中的sleep和wait的区别
Java多线程中Sleep与Wait的区别
共同点:
1. 他们都是在多线程的环境下,都可以在程序的调用处阻塞指定的毫秒数,并返回。
2. wait()和sleep()都可以通过interrupt()方法 打断线程的暂停状态 ,从而使线程立刻抛出InterruptedException。
如果线程A希望立即结束线程B,则可以对线程B对应的Thread实例调用interrupt方法。如果此刻线程B正在wait/sleep/join,则线程B会立刻抛出InterruptedException,在catch() {} 中直接return即可安全地结束线程。
需要注意的是,InterruptedException是线程自己从内部抛出的,并不是interrupt()方法抛出的。对某一线程调用 interrupt()时,如果该线程正在执行普通的代码,那么该线程根本就不会抛出InterruptedException。但是,一旦该线程进入到 wait()/sleep()/join()后,就会立刻抛出InterruptedException 。
不同点:
1. Thread类的方法:sleep(),yield()等
Object的方法:wait()和notify()等
2. 每个对象都有一个锁来控制同步访问。Synchronized关键字可以和对象的锁交互,来实现线程的同步。
sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法。
3. wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在任何地方使用
4. sleep必须捕获异常,而wait,notify和notifyAll不需要捕获异常
sleep和wait的区别
sleep和wait的区别: 1、sleep的意思是:睡,睡觉,睡眠。 2、wait的意思是:等候,推迟,延缓等待,耽搁,伺候用餐。 拓展资料 sleep的用法 1、They were exhausted from lack of sleep 由于缺乏睡眠,他们非常疲惫。 2、During the car journey, the baby slept 坐车来的路上,宝宝睡着了。 3、I think he may be ready for a sleep soon. 我想他也许很快就要睡一觉了。 4、I can't get to sleep with all that singing. 那些歌声搅得我无法入睡。 5、I didn't lose too much sleep over that investigation. 我并不太担心那个调查。 wait 1、I walk to a street corner and wait for the school bus 我走到街角等校车。 2、There'll be a car waiting for you 会有辆汽车等你。 3、I want to talk to you, but it can wait 我想和你谈谈,但可以晚点再说。 4、If you think this all sounds very exciting, just wait until you read the book 如果你觉得所有这些听起来令人兴奋,那就等着去读这本书吧。 5、'Wait a minute!' he broke in. 'This is not giving her a fair hearing!' “等一下,”他插嘴说,“这没有给她一个公平的解释机会!”
sleep与wait区别
sleep和wait的区别: 1、sleep的意思是:睡,睡觉,睡眠。 2、wait的意思是:等候,推迟,延缓等待,耽搁,伺候用餐。 拓展资料 sleep的用法 1、They were exhausted from lack of sleep 由于缺乏睡眠,他们非常疲惫。 2、During the car journey, the baby slept 坐车来的路上,宝宝睡着了。 3、I think he may be ready for a sleep soon. 我想他也许很快就要睡一觉了。 4、I can't get to sleep with all that singing. 那些歌声搅得我无法入睡。 5、I didn't lose too much sleep over that investigation. 我并不太担心那个调查。 wait 1、I walk to a street corner and wait for the school bus 我走到街角等校车。 2、There'll be a car waiting for you 会有辆汽车等你。 3、I want to talk to you, but it can wait 我想和你谈谈,但可以晚点再说。 4、If you think this all sounds very exciting, just wait until you read the book 如果你觉得所有这些听起来令人兴奋,那就等着去读这本书吧。 5、'Wait a minute!' he broke in. 'This is not giving her a fair hearing!' “等一下,”他插嘴说,“这没有给她一个公平的解释机会!”
详解sleep和wait的区别
sleep和wait的区别: 1、sleep的意思是:睡,睡觉,睡眠。 2、wait的意思是:等候,推迟,延缓等待,耽搁,伺候用餐。 拓展资料 sleep的用法 1、They were exhausted from lack of sleep 由于缺乏睡眠,他们非常疲惫。 2、During the car journey, the baby slept 坐车来的路上,宝宝睡着了。 3、I think he may be ready for a sleep soon. 我想他也许很快就要睡一觉了。 4、I can't get to sleep with all that singing. 那些歌声搅得我无法入睡。 5、I didn't lose too much sleep over that investigation. 我并不太担心那个调查。 wait 1、I walk to a street corner and wait for the school bus 我走到街角等校车。 2、There'll be a car waiting for you 会有辆汽车等你。 3、I want to talk to you, but it can wait 我想和你谈谈,但可以晚点再说。 4、If you think this all sounds very exciting, just wait until you read the book 如果你觉得所有这些听起来令人兴奋,那就等着去读这本书吧。 5、'Wait a minute!' he broke in. 'This is not giving her a fair hearing!' “等一下,”他插嘴说,“这没有给她一个公平的解释机会!”