生产者/消费者多种实现方法

1:使用wait/notify(notifyall)

package com.example.demo.demo.ThreadDemo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description: 生产、消费模式
 * 1、 使用 wait/notify
 * 存在的问题.1: java.lang.IllegalMonitorStateException 报错 原因:用了Object 的wait 和 notify 当某个线程试图等待一个自己并不拥有的对象(O)的监控器或者通知其他线程等待该对象(O)的监控器时,抛出该异常。
 * 2:  while(list.size() == 20) 这里必须使用while  不可以用if 会导致虚假唤醒线程
 * @create: 2020-03-30 10:12
 **/
public class ProductAndConsumeDemo {
    public static void main(String[] args) {
        List<ProductData> list = new ArrayList<>();

        for (int i = 0; i < 4; i++) {
            ExecutorService executorService = Executors.newCachedThreadPool();
            executorService.execute(new Product(list));
        }

        for (int i = 0; i < 6; i++) {
            ExecutorService executorService = Executors.newCachedThreadPool();
            executorService.execute(new Consume(list));
        }
        //生产者
       /* for (int i = 0; i < 4; i++) {
            Product product = new Product(list);
            Thread thread = new Thread(product);
            thread.start();
        }

        for (int i = 0; i < 6; i++) {
            Consume product = new Consume(list);
            Thread thread = new Thread(product);
            thread.start();
        }*/

    }

}

/**
 * @Description: 生产者
 * @Date: 2020/3/30  10:38
 */
class Product implements Runnable{
    //容器 这里用 ArrayList只是练习,其实真实生产中不推荐用它.对ArrayList做增删操作耗时.而且线程不安全
    private List<ProductData> list;

    public Product(List<ProductData> list) {
        this.list = list;
    }


    @Override
    public void run() {
        create();
    }

    /**
     * @Description: 生产数据
     * @param
     * @Author: chenxue
     * @Date: 2020/3/30  11:09
     */
    public void create(){
        synchronized (list){
            while(true){
                while(list.size() == 20){
                    try {
                        list.wait();
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                ProductData productData = ProductData.randomData();
                list.add(productData);
                System.out.println("添加数据为:" + productData.getName() + ":" + productData.getPrice());
                list.notify();
            }
        }
    }
}


/**
 * @Description: 消费者
 * @Date: 2020/3/30  10:38
 */
class Consume implements Runnable{
    //容器
    private List<ProductData> list;

    public Consume(List<ProductData> list) {
        this.list = list;
    }

    @Override
    public void run() {
        consumer();
    }

    //消费数据
    public void consumer(){
        synchronized (list){
            while (true){
                while (list.size() == 0){
                    try {
                        list.wait();
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //随机移除一个数据
                ProductData remove = list.remove(new Random().nextInt(list.size()));
                System.out.println("移除的数据为:" +remove.getName() + ":" + remove.getPrice());
                list.notify();
            }
        }
    }
}

/**
 * @Description: 数据
 * @Date: 2020/3/30  10:56
 */
class ProductData{
    public ProductData(String name, double price) {
        this.name = name;
        this.price = price;
    }

    // 产品名称
    private String name;
    //价格
    private double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public static ProductData randomData(){
        return new ProductData("" + new Random().nextInt(10),new Random().nextDouble()*1000);
    }
}

2:Lock实现:

package com.example.demo.demo.ThreadDemo;

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

/**
 * @description: Lock 实现生产消费者模式
 * @create: 2020-03-29 22:53
 **/
public class LockDemo {
    public static void main(String[] args) {
        LockDemo lockDemo = new LockDemo();
        for (int i = 0; i < 4; i++) {
            ExecutorService executorService = Executors.newCachedThreadPool();
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    lockDemo.increment();
                }
            });
        }

        for (int i = 0; i < 4; i++) {
            ExecutorService executorService = Executors.newCachedThreadPool();
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    lockDemo.decrement();
                }
            });
        }

    }
    //锁
    Lock lock = new ReentrantLock();
    //条件
    Condition condition = lock.newCondition();
    private final static Integer NUM = 10;
    private Integer current = 0;
    private List<Integer> list = new ArrayList<>();
    private boolean flag = true;
    /**
     * @Description: 生产者
     * @Date: 2020/3/30  14:49
     */
    public void increment(){
            try{
                lock.lock();
                while(flag){
                    while( list.size() == NUM){
                        System.out.println("缓存池满了,等待中...");
                        condition.await();
                    }
                    list.add(current ++);
                    System.out.println("缓冲池中添加:" + current);
                    condition.signalAll();
                }
            }catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
    }
    public void stop(){
        flag = false;
    }

    /**
     * @Description: 消费者
     * @Date: 2020/3/30  14:50
     */
    public void decrement(){
            try {
                lock.lock();
                while(flag){
                    while(list.size() == 0){
                        System.out.println("缓冲池中空了,等待中。。。");
                        condition.await();
                    }
                    list.remove(current --);
                    System.out.println("缓冲池中移除:" + current);
                    condition.signalAll();
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
    }

}

3.利用队列实现:

package com.example.demo.demo.ThreadDemo;

import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description: 使用阻塞队列 实现生产者、消费者模式
 * @create: 2020-03-30 23:04
 **/
public class BlockQueueDemo {
    public static void main(String[] args) {
        BlockData blockData = new BlockData();
        for (int i = 0; i < 4; i++) {
            ExecutorService executorService = Executors.newCachedThreadPool();
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        blockData.increment();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        for (int i = 0; i < 6; i++) {
            ExecutorService executorService = Executors.newCachedThreadPool();
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        blockData.decrement();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

}
class BlockData{
    private BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(MAX_NUM);
    private static final Integer MAX_NUM = 20;
    private boolean flag = true;
    private AtomicInteger atomicInteger = new AtomicInteger(0);

    //生产
    public void increment() throws InterruptedException {
        while (flag){
            boolean offer = blockingQueue.offer(atomicInteger.incrementAndGet());
            if(offer){
                System.out.println("生产数据成功:" + atomicInteger.get() + "队列长度:" + blockingQueue.size());
            }else {
                System.out.println("生产数据失败:" + atomicInteger.get() + "队列长度:" + blockingQueue.size());
            }
            Thread.sleep(1000);
        }
        System.out.println("停止生产!");
    }

    //消费
    public void decrement() throws InterruptedException {
        while (flag){
            Integer poll = blockingQueue.poll();
            System.out.println("消费数据:" + poll);
            Thread.sleep(1000);
        }
        System.out.println("停止消费!");
    }
    //停止
    public void stop(){
        this.flag = false;
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值