Condition实现生产者消费者模型

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author shy_black
 * @date 2019/3/16 18:28
 * @Description:
 *  Condition实现生产消费者模型
 */

class Goods {
    private int maxCount;
    private String name;
    private int count;

    Lock lock = new ReentrantLock();
    Condition producterCondition = lock.newCondition();
    Condition consumerCondition = lock.newCondition();

    public Goods(int maxCount) {
        this.maxCount = maxCount;
    }

    //制造商品
    public void setGoods(String name) {
        lock.lock();
        try {
            while (count == maxCount) {
                System.out.println(Thread.currentThread().getName() + "商品已达到最大数量");
                try {
                    producterCondition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.name = name;
            count++;
            System.out.println(Thread.currentThread().getName() + "生产" + toString());
            consumerCondition.signalAll();
        } finally {
            lock.unlock();
        }
    }
    //消费商品
    public void getGood() {
        lock.lock();
        try {
            while (count == 0) {
                System.out.println(Thread.currentThread().getName() + "已售罄");
                try {
                    consumerCondition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            count--;
            System.out.println(Thread.currentThread().getName() + "消费了" + toString());
            producterCondition.signalAll();
        } finally {
            lock.unlock();
        }
    }

    @Override
    public String toString() {
        return "Goods = " + name + "---" + " count = " + count;
    }
}

class Producter implements Runnable {
    private Goods goods;

    public Producter(Goods goods) {
        this.goods = goods;
    }

    @Override
    public void run() {
        while (true)
            this.goods.setGoods("特百惠");
    }
}

class Consumer implements Runnable {
    private Goods goods;

    public Consumer(Goods goods) {
        this.goods = goods;
    }

    @Override
    public void run() {
        while (true)
            this.goods.getGood();
    }
}

public class Condition_test {
    public static void main(String[] args) {
        Goods goods = new Goods(10);
        Producter producter = new Producter(goods);
        Consumer consumer = new Consumer(goods);
        List<Thread> list = new ArrayList<>();

        for (int i = 1; i <= 5; i++) {
            Thread thread = new Thread(producter, "生产者" + i);
            list.add(thread);
        }
        for (int i = 1; i <= 10; i++) {
            Thread thread = new Thread(consumer, "消费者" + i);
            list.add(thread);
        }
        for (Thread thread : list) {
            thread.start();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值