亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JAVA 線程通信相關知識匯總

 更新時間:2020年06月16日 16:39:19   作者:追  
這篇文章主要介紹了JAVA 線程通信相關知識,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下

兩個線程之間的通信

多線程環(huán)境下CPU會隨機的在線程之間進行切換,如果想讓兩個線程有規(guī)律的去執(zhí)行,那就需要兩個線程之間進行通信,在Object類中的兩個方法wait和notify可以實現(xiàn)通信。

wait方法可以使當前線程進入到等待狀態(tài),在沒有被喚醒的情況下,線程會一直保持等待狀態(tài)。
notify方法可以隨機喚醒單個在等待狀態(tài)下的線程。

來實現(xiàn)這樣的一個功能:
讓兩個線程交替在控制臺輸出一行文字

定義一個Print類,有兩個方法print1和print2,分別打印一行不同的內容

package com.sutaoyu.volatlt;

public class Print {
 private int flag = 1;
 
 public void print1() {
  synchronized(this) {
   if(flag != 1) {
    try {
     //讓當前線程進入等入狀態(tài)
     this.wait();
    }catch(InterruptedException e) {
     e.printStackTrace();
    }
   }
   
   System.out.println("monkey");
   flag = 2;
   //隨機的喚醒單個等待的線程
   this.notify();
  }
 }
 
 
 public void print2() {
  synchronized(this) {
   if(flag != 2) {
   try {
    this.wait();
   }catch (InterruptedException e){
    e.printStackTrace();
   }
  }
  System.out.println("1024");
  flag = 1;
  this.notify();
  }
 }
}

定義線程測試類,開啟兩個線程,分別運行Print類中print1和print2方法

package com.sutaoyu.volatlt;

public class NotifyTest01 {
 public static void main(String[] args) {
  Print p = new Print();
  Thread t1 = new Thread() {
   public void run() {
    while(true) {
     p.print1();
    }
   }
  };
  
  Thread t2 = new Thread() {
   public void run() {
    while(true) {
     p.print2();
    }
   }
  };
  t1.start();
  t2.start();
 }
}

三個及三個以上的線程之間的通信

改造上面代碼在Print類中添加一個print3方法,再開啟第三個線程來執(zhí)行這個方法。

另外需要修改的地方是:

1.因為notifyAll方法可以喚醒所有等待狀態(tài)的線程,所有用notifyAll方法來替代notify方法

2.當線程被喚醒后,需要先判斷一下flag的值,if不會重新判斷flag值,而while會重新判斷flag的值,所以將Print中的if判斷修改為while判斷。

package com.sutaoyu.volatlt;

public class Print {
 private int flag = 1;
 
 public void print1() {
  synchronized(this) {
   while(flag != 1) {
    try {
     //讓當前線程進入等入狀態(tài)
     this.wait();
    }catch(InterruptedException e) {
     e.printStackTrace();
    }
   }
   
   System.out.println("monkey");
   flag = 2;
   //隨機的喚醒單個等待的線程
   this.notifyAll();
  }
 }
 
 
 public void print2() {
  synchronized(this) {
   while(flag != 2) {
   try {
    this.wait();
   }catch (InterruptedException e){
    e.printStackTrace();
   }
  }
  System.out.println("1024");
  flag = 3;
  this.notifyAll();
  }
 }
 
 public void print3() {
  synchronized(this) {
   while(flag != 3) {
    try {
     this.wait();
    }catch(InterruptedException e) {
     e.printStackTrace();
    }
   }
   
   System.out.println("888");
   flag = 1;
   this.notifyAll();
  }
 }
}
package com.sutaoyu.volatlt;

public class NotifyTest01 {
 public static void main(String[] args) {
  Print p = new Print();
  Thread t1 = new Thread() {
   public void run() {
    while(true) {
     p.print1();
    }
   }
  };
  
  Thread t2 = new Thread() {
   public void run() {
    while(true) {
     p.print2();
    }
   }
  };
  
  Thread t3 = new Thread() {
   public void run() {
    while(true) {
     p.print3();
    }
   }
  };
  
  t1.start();
  t2.start();
  t3.start();
 }
}

線程通信注意事項

在print1,2,3方法中同步代碼塊中使用哪個對象作為鎖,那在調用wait和notify方法時一定要調用這個對象上的wait和notify方法。
上面程序使用this作為對象鎖,在下面調用的都是this.wait()和this.notify()方法。

在多線程執(zhí)行當中
wait方法釋放對象鎖,根據(jù)上面的代碼示例,t1,t2,t3三個線程使用的是同一個對象鎖,如果wait方法不釋放鎖的話,別的線程就不能獲取到該鎖,也就不能獲取cpu的執(zhí)行權了。

sleep和notify方法不釋放對象鎖,上面代碼示例中,如果notify方法釋放鎖的話,別的線程就有可能獲取到cpu的執(zhí)行權,這樣子就會導致當前notify方法后面的代碼還未執(zhí)行完畢就失去了cpu的執(zhí)行權,從而導致一些問題,只有當線程執(zhí)行完synchronized代碼塊后才會釋放鎖。

以上就是JAVA 線程通信相關知識匯總的詳細內容,更多關于JAVA 線程通信的資料請關注腳本之家其它相關文章!

相關文章

  • Java實現(xiàn)兩人五子棋游戲(三) 畫出棋子

    Java實現(xiàn)兩人五子棋游戲(三) 畫出棋子

    這篇文章主要為大家詳細介紹了Java實現(xiàn)兩人五子棋游戲,畫出五子棋的棋子,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction

    spring啟動錯誤Singleton bean creation not al

    本文主要介紹了spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • java實現(xiàn)簡單租車系統(tǒng)

    java實現(xiàn)簡單租車系統(tǒng)

    這篇文章主要為大家詳細介紹了java實現(xiàn)簡單租車系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • springboot?filter配置多個時,執(zhí)行順序問題

    springboot?filter配置多個時,執(zhí)行順序問題

    這篇文章主要介紹了springboot?filter配置多個時,執(zhí)行順序問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 實例分析Java Class的文件結構

    實例分析Java Class的文件結構

    今天把之前在Evernote中的筆記重新整理了一下,發(fā)上來供對java class 文件結構的有興趣的同學參考一下
    2013-04-04
  • JavaWeb倉庫管理系統(tǒng)詳解

    JavaWeb倉庫管理系統(tǒng)詳解

    這篇文章主要為大家詳細介紹了JavaWeb倉庫管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • java線程之死鎖

    java線程之死鎖

    這篇文章主要介紹了Java線程之死鎖,死鎖是這樣一種情形-多個線程同時被阻塞,它們中的一個或者全部都在等待某個資源被釋放。由于線程被無限期地阻塞,因此程序不可能正常終止
    2022-05-05
  • Jenkins忘記密碼密碼重置操作步驟詳解

    Jenkins忘記密碼密碼重置操作步驟詳解

    這篇文章主要為大家介紹了Jenkins忘記密碼密碼重置操作步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • java多線程之定時器Timer的使用詳解

    java多線程之定時器Timer的使用詳解

    本篇文章主要介紹了java多線程之定時器Timer的使用詳解,Time類主要負責完成定時計劃任務的功能,有興趣的可以了解一下。
    2017-04-04
  • SpringBoot系列教程之防重放與操作冪等

    SpringBoot系列教程之防重放與操作冪等

    同一條數(shù)據(jù)被用戶點擊了多次,導致數(shù)據(jù)冗余,需要防止弱網(wǎng)絡等環(huán)境下的重復點擊,下面這篇文章主要給大家介紹了關于SpringBoot系列教程之防重放與操作冪等的相關資料,需要的朋友可以參考下
    2022-04-04

最新評論