Java-JUC(八):使用wait,notify|notifyAll完成生產者消費者通訊,虛假喚醒(Spurious Wakeups)問題出現情境,及問題解決方案。

來源:互聯網
上載者:User

標籤:分享   style   col   src   缺陷   生產   ack   public   包含   

類比通過線程實現消費者和訂閱者模式:

首先,定義一個店員:店員包含進貨、賣貨方法;其次,定義一個生產者,生產者負責給店員生產產品;再者,定義一個消費者,消費者負責從店員那裡消費產品。

店員:

/** * 店員 */class Clerk {    private int product = 0;    /**     * 進貨     */    public synchronized void purchase() {        if (product >= 10) {            System.out.println("產品已滿。。。");        } else {            System.out.println(Thread.currentThread().getName() + ":" + ++product);        }    }    /**     * 賣貨     */    public synchronized void sell() {        if (product <= 0) {            System.out.println("產品缺貨。。。");        } else {            System.out.println(Thread.currentThread().getName() + ":" + --product);        }    }}

生產者

/** * 生產者 不斷的生產產品給店員 * */class Productor implements Runnable{    private Clerk clerk;    public Productor(Clerk clerk){        this.clerk=clerk;    }        public void run() {        for(int i=0;i<20;i++){            clerk.purchase();        }            }}

消費者

/** * 消費者 不斷的從店員那裡消費產品 * */class Consumer implements Runnable{    private Clerk clerk;    public Consumer(Clerk clerk){        this.clerk=clerk;    }        public void run() {        for(int i=0;i<20;i++){            clerk.sell();        }            }}

此時,運行程式,運行結果如下:

Productor-A:1Productor-A:2Productor-A:3Productor-A:4Productor-A:5Productor-A:6Productor-A:7Productor-A:8Productor-A:9Productor-A:10產品已滿。。。產品已滿。。。產品已滿。。。產品已滿。。。產品已滿。。。產品已滿。。。產品已滿。。。產品已滿。。。產品已滿。。。產品已滿。。。Consumer-A:9Consumer-A:8Consumer-A:7Consumer-A:6Consumer-A:5Consumer-A:4Consumer-A:3Consumer-A:2Consumer-A:1Consumer-A:0產品缺貨。。。產品缺貨。。。產品缺貨。。。產品缺貨。。。產品缺貨。。。產品缺貨。。。產品缺貨。。。產品缺貨。。。產品缺貨。。。產品缺貨。。。

從運行列印結果可以發現這裡存在兩個問題:

1)一旦生產者發現店員產品已滿時,仍然沒有停止生產產品,在不斷地生產生產產品;

2)一旦消費者發現店員產品缺貨時,依然時不斷地消費消費。

這裡明顯是有缺陷的,現實中應該是:一旦發現貨物滿時,就不在進貨,而是開啟賣貨行為;當賣貨行為發現無貨時,開始進貨行為。

針對生產者消費者改進:

消費者、生產者、用戶端調用代碼不變,只修改店員類:

/** * 店員 */class Clerk {    private int product = 0;    /**     * 進貨     */    public synchronized void purchase() {        if (product >= 10) {            System.out.println("產品已滿。。。");            try {                this.wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        } else {            System.out.println(Thread.currentThread().getName() + ":" + ++product);            this.notifyAll();        }    }    /**     * 賣貨     */    public synchronized void sell() {        if (product <= 0) {            System.out.println("產品缺貨。。。");            try {                this.wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        } else {            System.out.println(Thread.currentThread().getName() + ":" + --product);            this.notifyAll();        }    }}

此時運行結果:

Productor-A:1Consumer-A:0產品缺貨。。。Productor-A:1Productor-A:2Productor-A:3Productor-A:4Productor-A:5Productor-A:6Productor-A:7Productor-A:8Productor-A:9Productor-A:10產品已滿。。。Consumer-A:9Consumer-A:8Consumer-A:7Consumer-A:6Consumer-A:5Consumer-A:4Consumer-A:3Consumer-A:2Consumer-A:1Consumer-A:0產品缺貨。。。Productor-A:1Productor-A:2Productor-A:3Productor-A:4Productor-A:5Productor-A:6Productor-A:7Productor-A:8Consumer-A:7Consumer-A:6Consumer-A:5Consumer-A:4Consumer-A:3Consumer-A:2Consumer-A:1

此時,從結果運行來說是按照我們希望的結果出現了。

改進後帶來問題一:

修改店員類的最大進貨數為1,把生產者一次生產20修改為2,消費者一次消費20也修改為2。

店員類:

/** * 店員 */class Clerk {    private int product = 0;    /**     * 進貨     */    public synchronized void purchase() {        if (product >= 1) {            System.out.println("產品已滿。。。");            try {                this.wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        } else {            System.out.println(Thread.currentThread().getName() + ":" + ++product);            this.notifyAll();        }    }    /**     * 賣貨     */    public synchronized void sell() {        if (product <= 0) {            System.out.println("產品缺貨。。。");            try {                this.wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        } else {            System.out.println(Thread.currentThread().getName() + ":" + --product);            this.notifyAll();        }    }}

此時運行結果:

從運行結果上來看,程式是一個死結現象。

為什麼會發生死結問題?

從運行結果上來看分析:

 

 

改進後帶來問題二:

 

Java-JUC(八):使用wait,notify|notifyAll完成生產者消費者通訊,虛假喚醒(Spurious Wakeups)問題出現情境,及問題解決方案。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.