生产者消费者问题

最简单的多线程问题

使用sychronized

生产者

public class Producer implements Runnable{
    private Goods goods;

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

    @Override
    public void run() {
        for (int i = 0; i < 25; i++) {
                goods.make("旺仔","小馒头");
        }
    }
}

消费者

public class Consumer implements Runnable{

    private Goods goods;

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

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            goods.sell("旺仔","小馒头");
        }
    }
}

商品

public class Goods {
    private int quantity;// 库存
    private String brand;
    private String name;

    public Goods() {
    }

    public Goods( String brand, String name) {
        this.brand = brand;
        this.name = name;
    }

    // 消费商品
    public synchronized void sell(String brand,String name)   {
        if (quantity == 0){// 库存没了等待生产
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        quantity--;
        System.out.println("消费了"+brand+name + "--现在库存"+quantity);
        notify(); // 唤醒
    }
    // 生产商品
    public synchronized void make(String brand,String name) {
        if (quantity == 10){// 仓库满了,等待销售
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        quantity++;
        System.out.println("生产了"+brand+name +  "--现在库存"+quantity);
        notify(); // 唤醒
    }

    @Override
    public String toString() {
        return "Goods{" +
                "quantity=" + quantity +
                ", brand='" + brand + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

测试类

Goods goods =new Goods();
Producer producer=new Producer(goods);
Consumer consumer = new Consumer(goods);
Thread t1 = new Thread(producer);
Thread t2 = new Thread(consumer);
t2.start();
t1.start();

使用JUC包中的 BlockingQueue

生产者

public class ProducerQueue implements Runnable{
    private BlockingQueue<Goods> blockingQueue;

    public ProducerQueue(BlockingQueue<Goods> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            Goods goods = new Goods("wahaha","矿泉书");
            System.out.println("生产者生产商品" + goods);
            try {
                blockingQueue.put(goods);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

消费者

public class ConsumerQueue implements Runnable {
    private BlockingQueue<Goods> blockingQueue;

    public ConsumerQueue(BlockingQueue<Goods> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                Goods goods = blockingQueue.take();
                System.out.println("消费者消费商品" + goods);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

测试类

Goods goods =new Goods();

BlockingQueue<Goods> queue = new ArrayBlockingQueue<>(5);

ProducerQueue producerQueue = new ProducerQueue(queue);
ConsumerQueue consumerQueue = new ConsumerQueue(queue);

Thread t1 = new Thread(consumerQueue);
Thread t2 = new Thread(producerQueue);
t1.start();
t2.start();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值