午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

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

java - 關于多線程notify的問題

瀏覽:166日期: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午夜在线 | 四色成人av永久网址 | 国产中文字幕一区二区三区 | 成年人免费网站在线观看 | 日本三级视频网站 | 日日夜夜一区二区 | 天天舔天天操 | 国产中文字幕在线视频 | 一二区精品 | 亚洲成年人网 | 激情综合婷婷 | 性欧美精品 | 激情五月婷婷丁香 | 我想看黄色一级片 | 天天伊人网 | 亚洲免费黄色 | av免费在线网站 | 日韩精品片 | 大片在线观看网站免费收看 | 蜜桃视频黄色 | 久久成人国产 | 婷婷五月色综合 | 国产a网站 | 九九欧美 | 成年人视频在线看 | 麻豆网站在线观看 | 一区三区在线观看 | 国产精品久久婷婷六月丁香 | 伊人久久大香线蕉综合网站 | 四虎永久 | 91免费视频网站 | 久久综合视频网 | 超碰狠狠操 | 精品国产福利 | 可以在线观看av的网站 | 91久久国产视频 | 久久免费影院 | 福利在线免费观看 | 国产成人av一区二区三区 |