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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
SpringMVC之AbstractAnnotationConfigDispatcherSer解读
这篇文章主要介绍了SpringMVC之AbstractAnnotationConfigDispatcherSer,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-05-05java线程池ThreadPoolExecutor的八种拒绝策略示例详解
ThreadPoolExecutor是一个典型的缓存池化设计的产物,因为池子有大小,当池子体积不够承载时,就涉及到拒绝策略。JDK中已预设了 4 种线程池拒绝策略,下面结合场景详细聊聊这些策略的使用场景以及还能扩展哪些拒绝策略2021-11-11解决接口调用报错newSocketStream(..)failed:Too many open files问题
这篇文章主要介绍了解决接口调用报错newSocketStream(..)failed:Too many open files问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-07-07
最新评论