
在java的Object類中有三個final的方法允許線程之間進行資源對象鎖的通信,他們分別是: wait(), notify() ,notifyAll()。
調用這些方法的當前線程必須擁有此對象監視器,否則將會報java.lang.IllegalMonitorStateException exception異常。
Object的wait方法有三個重載方法,其中一個方法wait() 是無限期(一直)等待,直到其它線程調用notify或notifyAll方法喚醒當前的線程;另外兩個方法wait(long timeout) 和wait(long timeout, int nanos)允許傳入 當前線程在被喚醒之前需要等待的時間,timeout為毫秒數,nanos為納秒數。
notify方法只喚醒一個等待(對象的)線程并使該線程開始執行。所以如果有多個線程等待一個對象,這個方法只會喚醒其中一個線程,選擇哪個線程取決于操作系統對多線程管理的實現。
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
............
新聞熱點
疑難解答