生产者消费者模式-java实现

仓库

public class MyQueue {
    private static List<String> queueList = Collections.synchronizedList(new ArrayList<String>());

    /**
     *  循环次数
     */
    private volatile static int cycleCount = 30;

    /**
     *  生产者操作次数
     */
    public volatile static int proOperCount = 0;

    /**
     *  消费者操作次数
     */
    public volatile static int conOperCount = 0;

    /**
     *  仓库最大库存
     */
    public volatile static int maxStorageCount = 1;

    /**
     * 获取仓库对象
     * @return
     */
    public static List<String> getQueueList() {
        return queueList;
    }

    /**
     * 基于仓库对象当前线程等待
     */
    public static void queueWait(){
        try {
            queueList.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }catch (IllegalMonitorStateException m){
            m.printStackTrace();
        }
    }

    /**
     * 基于仓库对象当前线程指定时间等待
     * @param time
     */
    public static void queueWaitTime(int time){
        try {
            queueList.wait(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }catch (IllegalMonitorStateException m){
            m.printStackTrace();
        }
    }

    /**
     * 基于仓库对象唤醒所有线程
     */
    public static void queueNotifyAll(){
        try{
            queueList.notifyAll();
        }catch (IllegalMonitorStateException m){
            m.printStackTrace();
        }
    }

    public static int getCycleCount() {
        return cycleCount;
    }

    public static void setCycleCount(int cycleCount) {
        MyQueue.cycleCount = cycleCount;
    }

    public static int getMaxStorageCount() {
        return maxStorageCount;
    }

    public static void setMaxStorageCount(int maxStorageCount) {
        MyQueue.maxStorageCount = maxStorageCount;
    }

    public static int getProOperCount() {
        return proOperCount;
    }

    public synchronized static void setProOperCount(int proOperCount) {
        MyQueue.proOperCount = proOperCount;
    }

    public static int getConOperCount() {
        return conOperCount;
    }

    public synchronized static void setConOperCount(int conOperCount) {
        MyQueue.conOperCount = conOperCount;
    }
}

生产者

public class Producer implements Runnable {

    public void put() throws InterruptedException {
        /*
            判断是否超过了最大库存数
         */
        if (MyQueue.getQueueList().size() == MyQueue.getMaxStorageCount()) {
            System.out.println(Thread.currentThread().getName().toString()+"生产者等待");
            MyQueue.queueWaitTime(1);
        } else {
            /*
                增加生产者消费次数
             */
            int count = MyQueue.getProOperCount() + 1;
            MyQueue.setProOperCount(count);
            /*
                生产
             */
            String str = String.valueOf(Math.random());
            MyQueue.getQueueList().add(str);
            System.out.println(Thread.currentThread().getName().toString()+"生产:" + str + ", 生产数:"+count);
            /*
                等待,用于唤醒时和消费者公平竞争
             */
            MyQueue.queueWaitTime(1);
        }
    }

    @Override
    public void run() {
        try {
            /*
                按照指定的量进行生产
             */
            while (MyQueue.getProOperCount()< MyQueue.getCycleCount()) {
                /*
                    将仓库添加到线程监视器
                 */
                synchronized (MyQueue.getQueueList()) {
                    put();
                    /*
                        随机唤醒生产者或消费者
                     */
                    MyQueue.queueNotifyAll();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

消费者

public class Consumer implements Runnable {

    public void get() throws InterruptedException {
        /*
            判断仓库是否有货物
         */
        if (MyQueue.getQueueList().size() == 0) {
            System.out.println(Thread.currentThread().getName().toString()+"消费者等待");
            MyQueue.queueWaitTime(1);
        } else {
            /*
                增加消费计数
             */
            int count = MyQueue.getConOperCount() + 1;
            MyQueue.setConOperCount(count);
            /*
                消费
             */
            System.out.println(Thread.currentThread().getName().toString()+"消费:"+MyQueue.getQueueList().remove(0) + ", 消费数:"+count);
            /*
                等待,用于唤醒时和生产者公平竞争
             */
            MyQueue.queueWaitTime(1);
        }
    }

    @Override
    public void run() {
        try {
            /*
                按照指定的量进行消费
             */
            while (MyQueue.getConOperCount()< MyQueue.getCycleCount()) {
                /*
                    将仓库添加到线程监视器
                 */
                synchronized (MyQueue.getQueueList()) {
                    get();
                    /*
                        随机唤醒生产者或消费者
                     */
                    MyQueue.queueNotifyAll();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

生产者消费者入口

public class pandcDemo {
    public static void main(String[] args) {
        /*
            设置循环次数
         */
        MyQueue.setCycleCount(100);
        /*
            设置最大库存
         */
        MyQueue.setMaxStorageCount(1);

        Thread proOne = new Thread(new Producer());
        Thread proTwo = new Thread(new Producer());
        Thread proThree = new Thread(new Producer());
        Thread conOne = new Thread(new Consumer());
        Thread conTwo = new Thread(new Consumer());
        proOne.start();
        proTwo.start();
        proThree.start();
        conOne.start();
        conTwo.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值