Java object wait notify notifyAll代碼解析
測(cè)試代碼:
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..開始執(zhí)行wait..."); loc.wait(); System.out.println("["+sdf.format(new Date())+"] t1___loc..執(zhí)行wait后續(xù)..."); 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___進(jìn)入鎖..."); Thread.sleep(5000); System.out.println("["+sdf.format(new Date())+"] t3___loc..開始 wait..."); loc.wait(); System.out.println("["+sdf.format(new Date())+"] t3___loc..執(zhí)行 wait后續(xù)..."); 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..開始執(zhí)行 notify..."); loc.notify(); System.out.println("["+sdf.format(new Date())+"] t2___loc..執(zhí)行 notify后續(xù)..."); 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...."); }
執(zhí)行結(jié)果:
[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..開始執(zhí)行wait...[16:56:04 392] t2___獲得鎖...
[16:56:09 392] t2___loc..開始執(zhí)行 notify...
[16:56:09 392] t2___loc..執(zhí)行 notify后續(xù)...
[16:56:10 392] t2___loc 離開鎖....
[16:56:10 392] t3___進(jìn)入鎖...
[16:56:15 392] t3___loc..開始 wait...
[16:56:15 392] t1___loc..執(zhí)行wait后續(xù)...
[16:56:16 392] t1___loc..離開鎖...
t2___before join ....
t3____before join ....
總結(jié):
1. 執(zhí)行wait后‘'暫時(shí)‘ 釋放當(dāng)前對(duì)象鎖給其他線程,當(dāng)前線程處于等待狀態(tài)
2. syn塊中的wait收到notify通知后 喚醒cpu 繼續(xù)判斷鎖狀態(tài)
3. 執(zhí)行notify且當(dāng)前的對(duì)象鎖釋放后 wait等待的線程激活
4. notifyAll 是一次喚醒所有的wait
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java多線程通信wait()和notify()代碼實(shí)例
- Object類wait及notify方法原理實(shí)例解析
- Java多線程中的wait/notify通信模式實(shí)例詳解
- 分析java并發(fā)中的wait notify notifyAll
- java wait()/notify() 實(shí)現(xiàn)生產(chǎn)者消費(fèi)者模式詳解
- 基于線程的wait和notify使用,生產(chǎn)消費(fèi)案例
- 聊聊Object類中的wait()和notify()方法
- Java如何正確的使用wait-notify方法你知道嗎
- 為什么wait和notify必須放在synchronized中使用
相關(guān)文章
SpringMVC之AbstractAnnotationConfigDispatcherSer解讀
這篇文章主要介紹了SpringMVC之AbstractAnnotationConfigDispatcherSer,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Java利用Map實(shí)現(xiàn)計(jì)算文本中字符個(gè)數(shù)
這篇文章主要為大家詳細(xì)介紹了Java如何利用Map集合實(shí)現(xiàn)計(jì)算文本中字符個(gè)數(shù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-08-08java線程池ThreadPoolExecutor的八種拒絕策略示例詳解
ThreadPoolExecutor是一個(gè)典型的緩存池化設(shè)計(jì)的產(chǎn)物,因?yàn)槌刈佑写笮?當(dāng)池子體積不夠承載時(shí),就涉及到拒絕策略。JDK中已預(yù)設(shè)了?4?種線程池拒絕策略,下面結(jié)合場(chǎng)景詳細(xì)聊聊這些策略的使用場(chǎng)景以及還能擴(kuò)展哪些拒絕策略2021-11-11java注解的類型知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于java注解的類型知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03解決接口調(diào)用報(bào)錯(cuò)newSocketStream(..)failed:Too?many?open?files問題
這篇文章主要介紹了解決接口調(diào)用報(bào)錯(cuò)newSocketStream(..)failed:Too?many?open?files問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07基于Java實(shí)現(xiàn)互聯(lián)網(wǎng)實(shí)時(shí)聊天系統(tǒng)(附源碼)
Netty?是一個(gè)利用?Java?的高級(jí)網(wǎng)絡(luò)的能力,隱藏其背后的復(fù)雜性而提供一個(gè)易于使用的?API?的客戶端/服務(wù)器框架。本文將利用它實(shí)現(xiàn)互聯(lián)網(wǎng)實(shí)時(shí)聊天系統(tǒng),感興趣的可以了解一下2022-09-09JAVA 實(shí)現(xiàn)磁盤文件加解密操作的示例代碼
這篇文章主要介紹了JAVA 實(shí)現(xiàn)磁盤文件加解密操作的示例代碼,幫助大家利用Java實(shí)現(xiàn)文件的加解密,感興趣的朋友可以了解下2020-09-09