java多線程 生產者消費者案例-虛假喚醒

來源:互聯網
上載者:User

標籤:logs   notifyall   get   except   ups   des   oss   ted   Owner   

package com.java.juc;public class TestProductAndConsumer {    public static void main(String[] args) {        Clerk clerk = new Clerk();        Produce produce = new Produce(clerk);        Consumer consumer = new Consumer(clerk);        new Thread(produce, "線程A").start();        new Thread(consumer, "線程B").start();        new Thread(produce, "線程AA").start();        new Thread(consumer, "線程BB").start();    }}// 店員class Clerk {    private int product = 0;    public synchronized void get() {        while (product >= 1) {  //這裡使用while 不使用if 防止虛假喚醒            System.out.println("產品已滿!");            try {                this.wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        }        System.out.println(Thread.currentThread().getName() + " : " + ++product);        this.notifyAll();    }    public synchronized void sale() {        while (product <= 0) {            System.out.println("缺貨!");            try {                this.wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        }        System.out.println(Thread.currentThread().getName() + " : " + --product);        this.notifyAll();    }}// 生產者class Produce implements Runnable {    private Clerk clerk;    public Produce(Clerk clerk) {        this.clerk = clerk;    }    @Override    public void run() {        for (int i = 0; i < 20; i++) {            try {                Thread.sleep(200);            } catch (InterruptedException e) {                e.printStackTrace();            }            clerk.get();        }    }}// 消費者class Consumer implements Runnable {    private Clerk clerk;    public Consumer(Clerk clerk) {        this.clerk = clerk;    }    @Override    public void run() {        for (int i = 0; i < 20; i++) {            clerk.sale();        }    }}

 

  • public final void wait()                throws InterruptedException
    Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

    The current thread must own this object‘s monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object‘s monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

    As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

         synchronized (obj) {         while (<condition does not hold>)  //為了防止虛假喚醒應該總是使用在迴圈中             obj.wait();         ... // Perform action appropriate to condition     } 
    This method should only be called by a thread that is the owner of this object‘s monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

 

java多線程 生產者消費者案例-虛假喚醒

聯繫我們

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