并发编程中生产者与消费者几种实现方式

wait、notify 实现方式

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

class Producer extends Thread {

    private GoodsFactory goodsFactory;

    public Producer(GoodsFactory goodsFactory) {
        this.goodsFactory = goodsFactory;
    }

    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(new Random().nextInt(1000));
                goodsFactory.produce();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Consumer extends Thread {

    private GoodsFactory goodsFactory;

    public Consumer(GoodsFactory goodsFactory) {
        this.goodsFactory = goodsFactory;
    }

    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(new Random().nextInt(1000));
                goodsFactory.consume();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class GoodsFactory {

    private List<Integer> goods = new ArrayList<>();

    private AtomicInteger atomicInteger = new AtomicInteger();

    public synchronized void produce() throws InterruptedException {
        if (goods.size() >= 5) {
            System.out.println("商品已满,等待被消费!");
            wait();
        } else {
            goods.add(atomicInteger.incrementAndGet());
            System.out.println("生产了一个商品,当前共有: " + goods.size() + "个");
            notifyAll();
        }
    }

    public synchronized void consume() throws InterruptedException {
        if (goods.size() <= 0) {
            System.out.println("商品已空,等待生产!");
            wait();
        } else {
            goods.remove(0);
            System.out.println("消费了一个商品,当前共有: " + goods.size() + "个");
            notifyAll();
        }
    }

}

Lock、Condition 实现方式

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

class Producer extends Thread {

    private GoodsFactory goodsFactory;

    public Producer(GoodsFactory goodsFactory) {
        this.goodsFactory = goodsFactory;
    }

    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(new Random().nextInt(1000));
                goodsFactory.produce();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Consumer extends Thread {

    private GoodsFactory goodsFactory;

    public Consumer(GoodsFactory goodsFactory) {
        this.goodsFactory = goodsFactory;
    }

    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(new Random().nextInt(1000));
                goodsFactory.consume();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class GoodsFactory {

    private List<Integer> goods = new ArrayList<>();

    private AtomicInteger atomicInteger = new AtomicInteger();

    Lock lock = new ReentrantLock();
    Condition notFull = lock.newCondition();
    Condition notEmpty = lock.newCondition();

    public void produce() throws InterruptedException {
        try {
            lock.lock();
            if (goods.size() >= 5) {
                System.out.println("商品已满,等待被消费!");
                notFull.await();
            } else {
                goods.add(atomicInteger.incrementAndGet());
                System.out.println("生产了一个商品,当前共有: " + goods.size() + "个");
                notEmpty.signal();
            }
        } finally {
            lock.unlock();
        }
    }

    public void consume() throws InterruptedException {
        try {
            lock.lock();
            if (goods.size() <= 0) {
                System.out.println("商品已空,等待生产!");
                notEmpty.await();
            } else {
                goods.remove(0);
                System.out.println("消费了一个商品,当前共有: " + goods.size() + "个");
                notFull.signal();
            }
        }finally {
            lock.unlock();
        }
    }

}

LinkedBlockingDeque 实现方式

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

class Producer extends Thread {

    private GoodsFactory goodsFactory;

    public Producer(GoodsFactory goodsFactory) {
        this.goodsFactory = goodsFactory;
    }

    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(new Random().nextInt(1000));
                goodsFactory.produce();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Consumer extends Thread {

    private GoodsFactory goodsFactory;

    public Consumer(GoodsFactory goodsFactory) {
        this.goodsFactory = goodsFactory;
    }

    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(new Random().nextInt(1000));
                goodsFactory.consume();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class GoodsFactory {

    private AtomicInteger atomicInteger = new AtomicInteger();

    private BlockingDeque<Integer> blockingDeque = new LinkedBlockingDeque(5);

    public void produce() throws InterruptedException {

        blockingDeque.put(atomicInteger.incrementAndGet());
        System.out.println("生产了一个商品,当前共有: " + blockingDeque.size() + "个");

    }

    public void consume() throws InterruptedException {

        blockingDeque.take();
        System.out.println("消费了一个商品,当前共有: " + blockingDeque.size() + "个");

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码拉松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值