java生产者和消费者案例

来源:互联网 发布:本周国内经济数据 编辑:程序博客网 时间:2024/06/05 02:45

一、同步方法

public class TestProductorAndConsumer {    public static void main(String[] args) {        // TODO Auto-generated method stub        Clerk clerk = new Clerk();        Productor pro = new Productor(clerk);        Consumer cus = new Consumer(clerk);        new Thread(pro, "生产者A").start();        new Thread(cus, "消费者B").start();        new Thread(pro, "生产者C").start();        new Thread(cus, "消费者D").start();    }}// 店员class Clerk {    private int product = 0;    // 进货    public synchronized void get() {        while (product >= 1) { //为了避免虚假唤醒问题            System.out.println("产品已满");            try {                this.wait();            } catch (InterruptedException e) {                // TODO Auto-generated catch block                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) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        System.out.println(Thread.currentThread().getName() + ":" + --product);        this.notifyAll();    }}// 生产者class Productor implements Runnable {    private Clerk clerk;    public Productor(Clerk clerk) {        super();        this.clerk = clerk;    }    @Override    public void run() {        // TODO Auto-generated method stub        for (int i = 0; i < 20; i++) {            try {                Thread.sleep(200);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            clerk.get();        }    }}// 消费者class Consumer implements Runnable {    private Clerk clerk;    public Consumer(Clerk clerk) {        super();        this.clerk = clerk;    }    @Override    public void run() {        // TODO Auto-generated method stub        for (int i = 0; i < 20; i++) {            clerk.sale();        }    }}

二、Lock方式

public class TestProductorAndConsumerForLock {    public static void main(String[] args) {        // TODO Auto-generated method stub        ClerkLock clerk = new ClerkLock();        ProductorLock pro = new ProductorLock(clerk);        ConsumerLock cus = new ConsumerLock(clerk);        new Thread(pro, "生产者A").start();        new Thread(cus, "消费者B").start();        new Thread(pro, "生产者C").start();        new Thread(cus, "消费者D").start();    }}// 店员class ClerkLock {    private int product = 0;    private Lock lock = new ReentrantLock();    private Condition condition = lock.newCondition();    // 进货    public void get() {        lock.lock();        try {            while (product >= 1) { // 为了避免虚假唤醒问题                System.out.println("产品已满");                try {                    condition.await();                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            System.out.println(Thread.currentThread().getName() + ":"                    + ++product);            condition.signalAll();        } finally {            lock.unlock();        }    }    // 卖货    public void sale() {        lock.lock();        try {            while (product <= 0) {                System.out.println("缺货");                try {                    condition.await();                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            System.out.println(Thread.currentThread().getName() + ":"                    + --product);            condition.signalAll();        } finally {            lock.unlock();        }    }}// 生产者class ProductorLock implements Runnable {    private ClerkLock clerk;    public ProductorLock(ClerkLock clerk) {        super();        this.clerk = clerk;    }    @Override    public void run() {        // TODO Auto-generated method stub        for (int i = 0; i < 20; i++) {            try {                Thread.sleep(200);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            clerk.get();        }    }}// 消费者class ConsumerLock implements Runnable {    private ClerkLock clerk;    public ConsumerLock(ClerkLock clerk) {        super();        this.clerk = clerk;    }    @Override    public void run() {        // TODO Auto-generated method stub        for (int i = 0; i < 20; i++) {            clerk.sale();        }    }}