深入浅析Java项目中的object wait notify notifyAll?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
测试代码:
public static Object loc=new Object(); public static void main(String[] args) throws Exception{ Thread t1=new Thread(new Runnable() { @Override public void run() { try { java.text.SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss SSS"); System.out.println("["+sdf.format(new Date())+"] t1___等待锁..."); synchronized (loc) { System.out.println("["+sdf.format(new Date())+"] t1___获得锁 ..."); Thread.sleep(5000); System.out.println("["+sdf.format(new Date())+"] t1___loc..开始执行wait..."); loc.wait(); System.out.println("["+sdf.format(new Date())+"] t1___loc..执行wait后续..."); Thread.sleep(1000); } System.out.println("["+sdf.format(new Date())+"] t1___loc..离开锁..."); }catch (Exception e){ e.printStackTrace(); } } }); Thread t3=new Thread(new Runnable() { @Override public void run() { try { java.text.SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss SSS"); System.out.println("["+sdf.format(new Date())+"] t3___等待锁..."); synchronized (loc) { System.out.println("["+sdf.format(new Date())+"] t3___进入锁..."); Thread.sleep(5000); System.out.println("["+sdf.format(new Date())+"] t3___loc..开始 wait..."); loc.wait(); System.out.println("["+sdf.format(new Date())+"] t3___loc..执行 wait后续..."); Thread.sleep(1000); } System.out.println("["+sdf.format(new Date())+"] t3___离开锁..."); }catch (Exception e){ e.printStackTrace(); } } }); Thread t2=new Thread(new Runnable() { @Override public void run() { try { java.text.SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss SSS"); System.out.println("["+sdf.format(new Date())+"] t2___等待锁..."); synchronized (loc) { System.out.println("["+sdf.format(new Date())+"] t2___获得锁..."); Thread.sleep(5000); System.out.println("["+sdf.format(new Date())+"] t2___loc..开始执行 notify..."); loc.notify(); System.out.println("["+sdf.format(new Date())+"] t2___loc..执行 notify后续..."); Thread.sleep(1000); } System.out.println("["+sdf.format(new Date())+"] t2___loc 离开锁...."); }catch (Exception e){ e.printStackTrace(); } } }); t1.start(); t3.start(); Thread.sleep(500); t2.start(); System.out.println("t1___before join ...."); t1.join(); System.out.println("t2___before join ...."); t2.join(); System.out.println("t3____before join ...."); t3.join(); System.out.println("main exit...."); }
执行结果:
[16:55:59 384] t1___等待锁...
[16:55:59 384] t1___获得锁 ...
[16:55:59 384] t3___等待锁...
t1___before join ....
[16:55:59 836] t2___等待锁...
[16:56:04 392] t1___loc..开始执行wait...[16:56:04 392] t2___获得锁...
[16:56:09 392] t2___loc..开始执行 notify...
[16:56:09 392] t2___loc..执行 notify后续...
[16:56:10 392] t2___loc 离开锁....
[16:56:10 392] t3___进入锁...
[16:56:15 392] t3___loc..开始 wait...
[16:56:15 392] t1___loc..执行wait后续...
[16:56:16 392] t1___loc..离开锁...
t2___before join ....
t3____before join ....
总结:
1. 执行wait后‘'暂时‘ 释放当前对象锁给其他线程,当前线程处于等待状态
2. syn块中的wait收到notify通知后 唤醒cpu 继续判断锁状态
3. 执行notify且当前的对象锁释放后 wait等待的线程激活
4. notifyAll 是一次唤醒所有的wait
关于深入浅析Java项目中的object wait notify notifyAll问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。