java多线程 生产者消费者案例-虚假唤醒

Posted wq3435

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java多线程 生产者消费者案例-虚假唤醒相关的知识,希望对你有一定的参考价值。

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多线程 生产者消费者案例-虚假唤醒的主要内容,如果未能解决你的问题,请参考以下文章

多线程下虚假唤醒问题

juc高级特性——虚假唤醒 / Condition / 按序交替 / ReadWriteLock / 线程八锁

通过生产者消费者案例理解等待唤醒机制和虚假唤醒

生产者与消费者案例-虚假唤醒

生产者消费者和虚假唤醒

多线程编程中条件变量和的spurious wakeup 虚假唤醒