国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Java object wait notify notifyAll代碼解析

瀏覽:4日期:2022-08-21 08:06:00

測試代碼:

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___進入鎖...'); 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í)行結果:

[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___進入鎖...[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 ....

總結:

1. 執(zhí)行wait后‘’暫時‘ 釋放當前對象鎖給其他線程,當前線程處于等待狀態(tài)

2. syn塊中的wait收到notify通知后 喚醒cpu 繼續(xù)判斷鎖狀態(tài)

3. 執(zhí)行notify且當前的對象鎖釋放后 wait等待的線程激活

4. notifyAll 是一次喚醒所有的wait

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Java
相關文章:
主站蜘蛛池模板: 庐江县| 镇远县| 阿鲁科尔沁旗| 洞口县| 石台县| 和平县| 通州市| 纳雍县| 措勤县| 南漳县| 利川市| 齐河县| 大关县| 中西区| 黔江区| 名山县| 拉萨市| 高淳县| 平顺县| 揭西县| 上犹县| 房山区| 柳江县| 平远县| 龙泉市| 江油市| 武平县| 桂阳县| 广宗县| 克什克腾旗| 卢龙县| 维西| 晋江市| 宝应县| 黑龙江省| 漳平市| 贵州省| 佛坪县| 泾川县| 嘉定区| 南涧|