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. 基于JavaScript实现贪吃蛇游戏2. javascript单张多张图无缝滚动实例代码3. PHP 工程师面试的四个环节4. JSR-220:EJB 3.0 最终草案出台5. Django中如何使用Channels功能6. Spring Boot如何支持嵌入式Servlet容器7. 解析Spring Boot内嵌tomcat关于getServletContext().getRealPath获取得到临时路径的问题8. vue用elementui写form表单时,在label里添加空格操作9. vue 获取到数据但却渲染不到页面上的解决方法10. 基于Spring AOP proxyTargetClass的行为表现总结

网公网安备