安卓进阶(5)之消费者/生产者模式的3种实现方式

实现的接口

/**
 * 作者:luoxiaohui
 * 日期:2018/10/9 09:02
 * 文件描述: 接口,消费和生产方法
 */
public interface Person {

    void consume(String personName) throws InterruptedException;
    void produce(String personName) throws InterruptedException;
}

消费者类

/**
 * 作者:luoxiaohui
 * 日期:2018/10/8 19:34
 * 文件描述: 消费者
 */
public class Consumer implements Runnable {

    private Person person;
    private String personName;

    public Consumer(Person person, String personName) {
        this.person = person;
        this.personName = personName;
    }

    @Override
    public void run() {

        try {
            person.consume(personName);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

生产者类

/**
 * 作者:luoxiaohui
 * 日期:2018/10/8 19:33
 * 文件描述: 生产者
 */
public class Producer implements Runnable {

    private Person person;
    private String personName;

    public Producer(Person person, String personName) {
        this.person = person;
        this.personName = personName;
    }

    @Override
    public void run() {

        try {
            person.produce(personName);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

synchronized+wait/notify实现

/**
 * 作者:luoxiaohui
 * 日期:2018/10/8 19:02
 * 文件描述: 使用同步关键字和wait实现生产者消费者模型
 */
public class SyncPerson implements Person{
    
    private static final Object obj = new Object();
    private int bytCount = 0;

    public void produce(String personName) throws InterruptedException {

        synchronized (obj) {
            while (true) {

                if (bytCount == 5) {

                    obj.notifyAll();
                    System.out.println(personName + "已经生产了足够多的避孕套,等待被消费。。。");
                    Thread.sleep(1000);
                    obj.wait();
                }
                bytCount++;
                System.out.println(personName + "生产了" + bytCount + "只避孕套");
                Thread.sleep(1000);
            }
        }
    }

    public void consume(String personName) throws InterruptedException {

        synchronized (obj) {
            while (true) {

                if (bytCount == 0) {

                    obj.notifyAll();
                    System.out.println(personName + "已将避孕套消费完,需要再生产。。。");
                    Thread.sleep(1000);
                    obj.wait();

                }
                bytCount--;
                System.out.println(personName + "消费了" + (5 - bytCount) + "只避孕套");
                Thread.sleep(1000);
            }
        }
    }
}

ReentrantLock实现

/**
 * 作者:luoxiaohui
 * 日期:2018/10/8 20:25
 * 文件描述: 使用Lock+condition实现生产者/消费者模式
 */
public class LockPerson implements Person{

    private ReentrantLock lock = new ReentrantLock();
    private Condition consumeCondition = lock.newCondition();
    private Condition produceCondition = lock.newCondition();
    private int bytCount = 0;

    @Override
    public void produce(String personName) throws InterruptedException {

        try{
            lock.lock();
            while (true){
                if(bytCount == 5){

                    Thread.sleep(1000);
                    System.out.println(personName + "已经生产好避孕套,等待被消费。。。");
                    produceCondition.await();
                }
                bytCount++;
                System.out.println(personName + "生产了" + bytCount + "只避孕套");
                consumeCondition.signal();
                Thread.sleep(1000);
            }
        }finally {

            lock.unlock();
        }
    }

    @Override
    public void consume(String personName) throws InterruptedException {

        try{
            lock.lock();
            while (true){
                if(bytCount == 0){

                    Thread.sleep(1000);
                    System.out.println(personName + "已经消费完了避孕套,等待生产。。。");
                    consumeCondition.await();
                }
                bytCount--;
                System.out.println(personName + "消费了" + (5 - bytCount) + "只避孕套");
                produceCondition.signal();
                Thread.sleep(1000);
            }
        }finally {

            lock.unlock();
        }
    }
}

LinkedBlockingQueue实现

/**
 * 作者:luoxiaohui
 * 日期:2018/10/9 09:37
 * 文件描述:使用队列实现消费者生产者模式
 */
public class QueuePerson implements Person {

    private LinkedBlockingQueue queue = new LinkedBlockingQueue();

    @Override
    public void consume(String personName) throws InterruptedException {
        while (true) {

            System.out.println(personName + "消费了" + queue.take());
            Thread.sleep(1000);
        }
    }

    @Override
    public void produce(String personName) throws InterruptedException {

        String byt = "避孕套";
        while (true) {

            queue.put(byt);
            System.out.println(personName + "生产了" + byt);
            Thread.sleep(1000);
        }
    }
}

测试方法

//        SyncPerson person = new SyncPerson();
//        LockPerson person = new LockPerson();
        QueuePerson person = new QueuePerson();

        final Thread t1 = new Thread(new Producer(person, "我"));
        final Thread t2 = new Thread(new Consumer(person, "面试官"));
        
		t1.start();
        t2.start();
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值