java中的生产者消费者模式详解

方式 一: Synchronized方式
注:此种方式会造成资源的浪费:
利用锁的notifyAll()方法会将所有的线程都唤醒,会造成资源的浪费

class Resource {
    private String name;
    private int count = 1;
    private boolean flag = false;

    public synchronized void set(String name) {

        /*
            对于多个生产者和消费者,为什么要用while来判断标记?
            原因: 让要被唤醒的线程再判断一次标记,
                    因为现在的标记可能已经不是它原来等待时的状态了
         */
        while (flag) { 
            try {this.wait();} catch (Exception e) {}
        }
        this.name = name + "--" + count++;
        System.out.println(Thread.currentThread().getName() + "...Producer..." + this.name);
        this.flag = true;
        /*
            为什么要用notifyAll()
            因为需要唤醒对方的线程,如果只用notify()的方式有可能出现只唤醒本方线程的情况
            导致程序中的所有线程都处于等待的状态下!!!
         */
        this.notifyAll();
    }

    public synchronized void out() {
        while (!flag) {
            try {this.wait();} catch (Exception e) {}
        }
        System.out.println(Thread.currentThread().getName() + ".....Consumer....." + this.name);
        this.flag = false;
        this.notifyAll();
    }
}

class Producer implements Runnable {
    private Resource r;
    public Producer(Resource r) {
        this.r = r;
    }
    public void run() {
        while (true) {
            r.set("+Items+");
        }
    }
}

class Consumer implements Runnable {
    private Resource r;

    public Consumer(Resource r) {
        this.r = r;
    }

    public void run() {
        while (true) {
            r.out();
        }
    }
}


public class ProducerAndConsumer {
    public static void main(String[] args) {
        Resource r = new Resource();
        new Thread(new Producer(r)).start();
        new Thread(new Producer(r)).start();
        new Thread(new Consumer(r)).start();
        new Thread(new Consumer(r)).start();
    }
}

方式二:
利用Lock的方式显示的声明锁
这种方式可以人唤醒想要唤醒的线程,能够减少资源的浪费

/*
    JDK1.5中提供了多线程中锁的升级解决方案:
    将同步中的Synchronized替换成显式的lock操作
    一个锁可以对应多个Condition对象
    将object中的wait()、notify()、notifyAll()等方法替换成了Condition对象拥有的方法
    Condition对象通过lock锁的newCondition()方法来获取。
    不同类型的线程用不同的Condition对象来实现线程状态的切换,以达到对不同类型的线程进行不同的操作的目的
    例如:下面的示例中用condition对象来对生产者的线程进行操作,用conditionPro对象来对消费者的线程进行操作,
        这样就可以实现生产者的线程只唤醒消费者,消费者的线程只唤醒生产者,减少资源的浪费。
        即两种类型的线程用的同一把锁,但是用的不同的Condition对象,这样就可以实现本方只唤醒对方的线程的操作。
 */
import java.util.concurrent.locks.*;

class Resource {
    private String name;
    private int count = 1;
    private boolean flag = false;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private Condition conditionPro = lock.newCondition();

    public void set(String name) throws InterruptedException {
        lock.lock();
        try {
            while (flag)
                condition.await();
            this.name = name + "--" + count++;
            System.out.println(Thread.currentThread().getName() + "...Producer..." + this.name);
            this.flag = true;
            conditionPro.signal();
        } finally {
            lock.unlock();    // 释放锁的动作一定要执行,否则会造成抛异常之后不释放锁,别的线程拿不到锁
        }
    }

    public void out() throws InterruptedException {
        lock.lock();
        try {
            while (!flag)
                conditionPro.await();
            System.out.println(Thread.currentThread().getName() + ".....Consumer....." + this.name);
            this.flag = false;
            condition.signal();
        } finally {
            lock.unlock();
        }
    }
}

class Producer implements Runnable {
    private Resource r;
    public Producer(Resource r) {
        this.r = r;
    }
    public void run() {
        while (true) {
            try {r.set("+Items+");} catch (InterruptedException e) {}
        }
    }
}

class Consumer implements Runnable {
    private Resource r;

    public Consumer(Resource r) {
        this.r = r;
    }

    public void run() {
        while (true) {
            try {r.out();} catch (InterruptedException e) {}
        }
    }
}


public class ProducerAndConsumer {
    public static void main(String[] args) {
        Resource r = new Resource();
        new Thread(new Producer(r)).start();
        new Thread(new Producer(r)).start();
        new Thread(new Consumer(r)).start();
        new Thread(new Consumer(r)).start();
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值