java sleep()和wait()的区别点总结
wait()是Object的方法,sleep()是Thread的方法。
wait()必须采用同步方法,不需要sleep()方法。
线程在同步方法中执行sleep()方法,不释放monitor锁,wait()方法释放monitor锁。
短暂休眠后,sleep()方法会主动退出阻塞,而wait()方法需要在没有指定wait时间的情况下被其他线程中断才能退出阻塞。
2、实例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;/** * java中的sleep()和wait()的区别 * @author Hongten Java学习交流QQ群:589809992 我们一起学Java! * @date 2013-12-10 */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()的区别点总结的文章就介绍到这了,更多相关java sleep()和wait()的区别内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章:
1. IDEA的Mybatis Generator驼峰配置问题2. javascript设计模式 ? 建造者模式原理与应用实例分析3. 一篇文章带你了解JavaScript-对象4. Express 框架中使用 EJS 模板引擎并结合 silly-datetime 库进行日期格式化的实现方法5. Java构建JDBC应用程序的实例操作6. Python使用oslo.vmware管理ESXI虚拟机的示例参考7. IntelliJ Idea2017如何修改缓存文件的路径8. IntelliJ IDEA设置条件断点的方法步骤9. Jsp中request的3个基础实践10. 浅谈SpringMVC jsp前台获取参数的方式 EL表达式
