Java多線程之wait()和notify()

來源:互聯網
上載者:User

直接看測試代碼吧,細節之處,詳見注釋

package com.jadyer.thread.wait;/** * Java多線程之wait()和notify()的妙用 * @see ================================================================================================================= * @see 問題:同時啟動兩個線程和同時啟動四個線程,控制台列印結果是不同的 * @see      同時啟動兩個線程時,控制台會很規律的輸出1010101010101010 * @see      同時啟動四個線程時,控制台起初會規律的輸出10101010,一旦某一刻輸出一個負數,那麼後面的輸出就會"一錯再錯" * @see 分析:對線程而言,任何一種情況,都是合理的 * @see      這裡假設其中的一種情況:tt22先執行,此時number=0,所以執行到了decrease()方法中的wait()方法,於是tt22被阻塞 * @see      接著tt44執行了,此時number=0,所以也執行到了decrease()方法中的wait()方法,於是tt44也被阻塞了 * @see      然後tt11執行了,此時number=0,於是便執行到了increase()方法中的number++和notify()方法 * @see      重點在於tt11執行到notify()方法時,我們假設該方法喚醒了tt44線程,於是tt44開始執行decrease()方法中的number-- * @see      此時number=-1,然後執行到了decrease()方法中notify()方法,我們同樣假設該notify()方法喚醒的是tt22線程 * @see      同樣的道理,number又被減減了,於是number=-2,並被列印到控制台了,然後再假設tt22中的notify()方法喚醒的是tt11 * @see      如此的迴圈往複,就看到那種"一錯再錯"的效果了 * @see ================================================================================================================= * @see 修複:我們應當在wait()被喚醒的時候,再判斷一次,然後再決定是否讓該線程繼續wait()下去 * @see      因為,當某個線程被喚醒時,它不知道外界在其睡眠的期間發生了神馬,所以要再判斷一次。所以把if()改為while()判斷,即可 * @see ================================================================================================================= * @see 補充:如果只有兩個線程的話,一個是對number增加的線程,一個是對number減少的線程,此時用if()判斷是沒有問題的 * @see      因為無論線程如何的喚醒,它所喚醒的都是另一個線程,不存在第三個線程插進來搗亂的情況 * @see ================================================================================================================= * @author 宏宇 * @create Feb 22, 2012 3:20:05 PM */public class WaitNotifyTest {public static void main(String[] args) {Count count = new Count();Thread tt11 = new Thread(new IncreaseThread(count));Thread tt22 = new Thread(new DecreaseThread(count));Thread tt33 = new Thread(new IncreaseThread(count));Thread tt44 = new Thread(new DecreaseThread(count));tt11.start();tt22.start();tt33.start();tt44.start();}}class IncreaseThread implements Runnable{private Count count;public IncreaseThread(Count count){this.count = count;}@Overridepublic void run() {for(int i=0; i<20; i++){try {Thread.sleep((long)(Math.random()*1000));} catch (InterruptedException e) {e.printStackTrace();}count.increase();}}}class DecreaseThread implements Runnable{private Count count;public DecreaseThread(Count count){this.count = count;}@Overridepublic void run() {for(int i=0; i<20; i++){try {Thread.sleep((long)(Math.random()*1000));} catch (InterruptedException e) {e.printStackTrace();}count.decrease();}}}class Count{private int number;public synchronized void increase(){if(0 != number){try {//在同步方法(或者同步語句塊)中,被鎖定的對象可以調用wait()方法,這將導致當前線程被阻塞並釋放該對象的互斥鎖//即解除了wait()方法所對應的當前對象的鎖定狀態,然後,其它的線程就有機會訪問該對象了wait();} catch (InterruptedException e) {e.printStackTrace();}}number++;System.out.println(number);//喚醒其它的由於調用了wait()方法而在等待同一個對象的線程//該方法每次運行時,只能喚醒等待隊列中的一個線程,至於是哪一個線程被喚醒,則由線程調度器來決定,程式員無法控制notify();}public synchronized void decrease(){if(0 == number){try {wait();} catch (InterruptedException e) {e.printStackTrace();}}number--;System.out.println(number);notify();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.