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..開(kāi)始執(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..離開(kāi)鎖...');}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..開(kāi)始 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___離開(kāi)鎖...');}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..開(kāi)始執(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 離開(kāi)鎖....');}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..開(kāi)始執(zhí)行wait...
[16:56:04 392] t2___獲得鎖...[16:56:09 392] t2___loc..開(kāi)始執(zhí)行 notify...[16:56:09 392] t2___loc..執(zhí)行 notify后續(xù)...[16:56:10 392] t2___loc 離開(kāi)鎖....
[16:56:10 392] t3___進(jìn)入鎖...[16:56:15 392] t3___loc..開(kāi)始 wait...[16:56:15 392] t1___loc..執(zhí)行wait后續(xù)...[16:56:16 392] t1___loc..離開(kāi)鎖...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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Android table布局開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器2. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼3. 理解PHP5中static和const關(guān)鍵字4. php模擬實(shí)現(xiàn)斗地主發(fā)牌5. IntelliJ IDEA安裝插件的方法步驟6. spring acegi security 1.0.0 發(fā)布7. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)8. Python random庫(kù)使用方法及異常處理方案9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備