国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

傳統線程同步wait(),notify,notifyall()

2019-11-08 01:57:26
字體:
來源:轉載
供稿:網友

java的Object類中有三個final的方法允許線程之間進行資源對象鎖的通信,他們分別是: wait(), notify() ,notifyAll()。

調用這些方法的當前線程必須擁有此對象監視器,否則將會報java.lang.IllegalMonitorStateException exception異常。

wait

Object的wait方法有三個重載方法,其中一個方法wait() 是無限期(一直)等待,直到其它線程調用notify或notifyAll方法喚醒當前的線程;另外兩個方法wait(long timeout) 和wait(long timeout, int nanos)允許傳入 當前線程在被喚醒之前需要等待的時間,timeout為毫秒數,nanos為納秒數。

notify

notify方法只喚醒一個等待(對象的)線程并使該線程開始執行。所以如果有多個線程等待一個對象,這個方法只會喚醒其中一個線程,選擇哪個線程取決于操作系統對多線程管理的實現。

notifyAll

notifyAll 會喚醒所有等待(對象的)線程;

這些方法可以使用于“生產者-消費者”問題,消費者是在隊列中等待對象的線程,生產者是在隊列中釋放對象并通知其他線程的線程。

例子:

public class TraditionalThreadCommunication {/*** @param args*/public static void main(String[] args) {final Business business = new Business();new Thread(new Runnable() {@Overridepublic void run() {for(int i=1;i<=20;i++){business.a(i);}}}).start();new Thread(new Runnable() {@Overridepublic void run() {for(int i=1;i<=20;i++){business.b(i);}}}).start();new Thread(new Runnable() {@Overridepublic void run() {for(int i=1;i<=20;i++){try {Thread.sleep(3000);} catch (InterruptedException e) {e.PRintStackTrace();}business.c(i);}}}).start(); }}  class Business { private boolean bShouldSub = true; public synchronized void a(int i){ System.out.println("a-start"+i); try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}  }  public synchronized void b(int i){ System.out.println("b-start"+i); try {this.wait();} catch (InterruptedException e) {e.printStackTrace();} }    public synchronized void c(int i){ System.out.println("c-start"+i); this.notifyAll(); }   }

c線程每執行一次休眠3秒喚醒所有等待的線程:

輸出結果:

a-start1b-start1c-start1b-start2a-start2c-start2a-start3b-start3c-start3b-start4a-start4c-start4a-start5b-start5c-start5b-start6a-start6c-start6

............


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桓台县| 安义县| 周至县| 广州市| 仪征市| 休宁县| 上高县| 白水县| 普兰店市| 青冈县| 兴和县| 兴业县| 清远市| 玛纳斯县| 新昌县| 桦甸市| 探索| 渑池县| 河北区| 云龙县| 安远县| 东阳市| 南充市| 宝坻区| 东兴市| 延寿县| 沙雅县| 锡林郭勒盟| 土默特右旗| 山丹县| 泊头市| 苏尼特右旗| 麻城市| 利辛县| 浑源县| 富顺县| 师宗县| 金沙县| 桃源县| 武穴市| 永春县|