最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java object wait notify notifyAll代碼解析

 更新時間:2020年11月18日 10:09:29   作者:dint  
這篇文章主要介紹了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..開始執(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..離開鎖...");
        }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..執(zhí)行 wait后續(xù)...");
            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..開始執(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 離開鎖....");
        }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..開始執(zhí)行wait...

[16:56:04 392] t2___獲得鎖...
[16:56:09 392] t2___loc..開始執(zhí)行 notify...
[16:56:09 392] t2___loc..執(zhí)行 notify后續(xù)...
[16:56:10 392] t2___loc 離開鎖....


[16:56:10 392] t3___進入鎖...
[16:56:15 392] t3___loc..開始 wait...
[16:56:15 392] t1___loc..執(zhí)行wait后續(xù)...
[16:56:16 392] t1___loc..離開鎖...
t2___before join ....
t3____before join ....

總結(jié):

1. 執(zhí)行wait后‘'暫時‘ 釋放當前對象鎖給其他線程,當前線程處于等待狀態(tài)

2. syn塊中的wait收到notify通知后 喚醒cpu 繼續(xù)判斷鎖狀態(tài)

3. 執(zhí)行notify且當前的對象鎖釋放后 wait等待的線程激活

4. notifyAll 是一次喚醒所有的wait

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

布尔津县| 台州市| 孝感市| 卢氏县| 茶陵县| 青海省| 西乌珠穆沁旗| 曲靖市| 红河县| 集贤县| 全南县| 嵊州市| 十堰市| 岗巴县| 哈密市| 拉孜县| 邢台市| 云浮市| 怀安县| 榆中县| 武鸣县| 杭锦旗| 凤山县| 原阳县| 泰和县| 库车县| 湖口县| 凤庆县| 新田县| 砀山县| 宣化县| 济南市| 聂荣县| 株洲市| 安宁市| 宝山区| 洛隆县| 镇坪县| 沙雅县| 赞皇县| 宾阳县|