国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

java - 關于多線程notify的問題

瀏覽:80日期:2023-12-10 15:47:24

問題描述

public class WaitTest { static class ThreadA extends Thread {public ThreadA(String name){ super(name);}@Overridepublic void run() { synchronized (this){ System.out.println(Thread.currentThread().getName()+' call notify()'); //notify();//notify之后 要等到這個代碼塊結束之后才會把鎖讓出去,當然如果在notify之后又有wait,那就會主動把鎖讓出去 try { System.out.println(Thread.currentThread().getName()+' wait'); //wait(); //Thread.sleep(10000); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+' after notify'); }} } public static void main(String[] args) throws InterruptedException {ThreadA t1 =new ThreadA('t1');synchronized (t1){ System.out.println(Thread.currentThread().getName()+' start t1'); t1.start(); System.out.println(Thread.currentThread().getName()+' wait'); t1.wait();////System.out.println(Thread.currentThread().getName()+' notify'); // t1.notify(); System.out.println(t1.getName()); System.out.println(Thread.currentThread().getName()+' continue'); //t1.notify();} }}

照理來說 t1.wait() 應該會阻塞主線程,并沒有其他地方notify而去掉t1.start()之后,就能阻塞住了

這是什么道理?編譯器優化?還是synchronized代碼塊內如果不對monitor進行操作,結束主動notify??

問題解答

回答1:

并不是優化其實,跟線程的執行有關的。在java doc中,public final synchronized void join(long millis)這個方法的注釋上面寫著一句話

<p> This implementation uses a loop of {@code this.wait} calls conditioned on {@code this.isAlive}. As a thread terminates the {@code this.notifyAll} method is invoked. It is recommended that applications not use {@code wait}, {@code notify}, or {@code notifyAll} on {@code Thread} instances.

看到加黑體,其實是線程結束之后調用的notifyAll導致wait蘇醒的。并不是什么虛擬機優化導致的。希望能解答你的困惑

標簽: java
相關文章:
主站蜘蛛池模板: 国产成人小视频 | 成人手机看片 | 一级黄色录相片 | 成人69视频在线观看免费 | 91年精品国产福利线观看久久 | 日本特黄特色视频 | 手机毛片免费看 | 亚洲午夜在线 | 成人免费网站久久久 | 中美日韩在线网免费毛片视频 | 国内精品1区1区3区4区 | 成人人免费夜夜视频观看 | 一区二区影视 | 亚洲爱爱爱 | 三级中文字幕永久在线视频 | 萌白酱粉嫩jk福利视频在线观看 | 久久免费观看视频 | 久久香蕉国产线看观看式 | 国产在亚洲线视频观看 | 在线中文字幕亚洲 | 成人黄18免费网站 | 亚洲成人在线视频网站 | a级午夜毛片免费一区二区 a级性生活视频 | 欧美一区二区精品系列在线观看 | 日韩欧美黄色 | 久久在线视频 | 日本激情视频在线观看 | 国产福利久久 | 日韩一级a毛片欧美一级 | 国产成人精品日本亚洲专一区 | 久久久久久国产精品免费免 | 欧美成人三级伦在线观看 | 久草免费资源 | 日产一区两区三区四区 | 欧美一区亚洲二区 | 欧美怡红院免费全视频 | 久久国内精品自在自线400部o | 久久香蕉国产线看观看式 | 欧美一区二区三区精品国产 | 国产自在自线午夜精品视频 | 国产一级成人毛片 |