您好,登錄后才能下訂單哦!
深入淺析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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。