这篇文章主要介绍java中sleep()和wait()有什么区别,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Java是什么Java是一门面向对象编程语言,可以编写桌面应用程序、WEB应用程序、分布式系统和嵌入式系统
这篇文章主要介绍java中sleep()和wait()有什么区别,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
Java是一门面向对象编程语言,可以编写桌面应用程序、WEB应用程序、分布式系统和嵌入式系统应用程序。
wait()是Object的方法,sleep()是Thread的方法。
wait()必须采用同步方法,不需要sleep()方法。
线程在同步方法中执行sleep()方法,不释放monitor锁,wait()方法释放monitor锁。
短暂休眠后,sleep()方法会主动退出阻塞,而wait()方法需要在没有指定wait时间的情况下被其他线程中断才能退出阻塞。
import java.text.SimpleDateFORMat;import java.util.Date;public class TestSleepAndWait {public static void main(String[] args) {new Thread1().start();try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}new Thread2().start();}}class Thread1 extends Thread{private void sout(String s){System.out.println(s+" "+new SimpleDateFormat("HH:mm:ss:SS").format(new Date()));}@Overridepublic void run() {sout("enter Thread1.run");synchronized (TestSleepAndWait.class){//wait只能在同步代码块或者同步方法中使用sout("Thread1 is Going to wait");try {TestSleepAndWait.class.wait(); // 这里只能使用持有锁TestSleepAndWait.class.wait(),使用其他对象则报错java.lang.IllegalMonitorStateException} catch (InterruptedException e) {e.printStackTrace();}sout("after waiting, thread1 is going on");sout("thread1 is over");}}}class Thread2 extends Thread{private void sout(String s){System.out.println(s+" "+new SimpleDateFormat("HH:mm:ss:SS").format(new Date()));}@Overridepublic void run() {sout("enter Thread2.run");synchronized (TestSleepAndWait.class){//wait只能在同步代码块或者同步方法中使用sout("Thread2 is going to notify");TestSleepAndWait.class.notify(); 这里只能使用持有锁TestSleepAndWait.classsout("thread2 is going to sleep 10ms");try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}sout("after sleeping, thread2 is going on");sout("thread2 is over");}}}
内容扩展:
package com.b510.test;public class TestD { public static void main(String[] args) { new Thread(new Thread1()).start(); try { Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); } new Thread(new Thread2()).start(); } private static class Thread1 implements Runnable{ @Override public void run(){ synchronized (TestD.class) { System.out.println("enter thread1..."); System.out.println("thread1 is waiting..."); try { //调用wait()方法,线程会放弃对象锁,进入等待此对象的等待锁定池 TestD.class.wait(); } catch (Exception e) { e.printStackTrace(); } System.out.println("thread1 is going on ...."); System.out.println("thread1 is over!!!"); } } } private static class Thread2 implements Runnable{ @Override public void run(){ synchronized (TestD.class) { System.out.println("enter thread2...."); System.out.println("thread2 is sleep...."); //只有针对此对象调用notify()方法后本线程才进入对象锁定池准备获取对象锁进入运行状态。 TestD.class.notify(); //================== //区别 //如果我们把代码:TestD.class.notify();给注释掉,即TestD.class调用了wait()方法,但是没有调用notify() //方法,则线程永远处于挂起状态。 try { //sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程, //但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。 //在调用sleep()方法的过程中,线程不会释放对象锁。 Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); } System.out.println("thread2 is going on...."); System.out.println("thread2 is over!!!"); } } }}
以上是“java中sleep()和wait()有什么区别”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网精选频道!
--结束END--
本文标题: java中sleep()和wait()有什么区别
本文链接: https://www.lsjlt.com/news/275381.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0