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

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

java - 關于多線程notify的問題

瀏覽:100日期: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
相關文章:
主站蜘蛛池模板: 久久精品道一区二区三区 | 国产成人精品午夜视频' | 欧美成人午夜片一一在线观看 | 久久网在线| 国产亚洲高清在线精品99 | 欧美一级毛片激情 | 亚洲综合色一区二区三区小说 | 国产精品成人久久久久久久 | 国产真实孩交 | 黄色三级理沦片 | 色久天堂网 | 国产永久免费视频m3u8 | 成人禁在线观看网站 | 一级做a爰性色毛片免费 | 日本高清不卡在线观看 | 国产激情一区二区三区在线观看 | 成人毛片免费观看视频 | 一级淫 | 女人张开腿让男人添 | 欧美成年黄网站色视频 | 玖草在线视频 | 国产爽的冒白浆的视频高清 | 中文无码日韩欧免费视频 | 另类视频在线观看 | 99在线精品免费视频九九视 | 色视频在线观看免费 | 99久久精品免费看国产免费软件 | 久久精品国产一区二区三区不卡 | 欧美日韩顶级毛片www免费看 | 成人免费视频一区二区三区 | 日本加勒比视频在线观看 | 一本本久综合久久爱 | 亚洲bbbbbxxxxx精品三十七 | 亚洲欧美日韩在线线精品 | 亚洲在线免费 | 亚洲国产日韩欧美一区二区三区 | 精品亚洲一区二区 | 好看的看黄a大片爽爽影院 好男人天堂网 | 国产精品不卡 | 日本护士视频xxxxxwww | 天天综合天天看夜夜添狠狠玩 |