zoukankan      html  css  js  c++  java
  • 生产者消费者

    package com.charles.algorithm;
    
    public class ConsumerProducer {
        /**
         * @desc: single object implements producer and consumer by array
         */
        private final static int SIZE = 10;
        private int head = 0, tag = 0, count = 0;
        private Object[] items = new Object[SIZE];
    
        public static void main(String[] args) {
    
            // define 5 producers
            ConsumerProducer conducer = new ConsumerProducer();
            for (int i = 0; i < SIZE/2; i++) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            conducer.produce();
                        }
                    }
                }).start();
            }
            // define 3 consumers
            for (int i = 0; i < SIZE/3; i++) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            conducer.consume();
                        }
                    }
                }).start();
            }
        }
    
        public synchronized void produce() {
            while (isFull()) {
                try {
                    System.out.println("生产者-"+Thread.currentThread().getName() + " 已满:" + count +" 等代销费者");
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
            items[tag++] = true;
            count++;
            if (tag == items.length) {
                tag = 0;
            }
            this.notifyAll();
            try {
                System.out.println("生产者-"+Thread.currentThread().getName() + " 生产一个,剩余" + count);
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public synchronized void consume() {
            while (isEmpty()) {
                try {
                    System.out.println("消费者-"+Thread.currentThread().getName() + " 已空:" + count+" 等代生产者");
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
            items[head] = null;
            head++;
            count--;
            if (head == items.length) {
                head = 0;
            }
            this.notifyAll();
            try {
                System.out.println("消费者-"+Thread.currentThread().getName() + " 消费一个,还有" + count);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        private boolean isFull() {
            return count == items.length;
        }
    
        private boolean isEmpty() {
            return 0 == count;
        }
    }
  • 相关阅读:
    为Docker容器配置固定IP
    Docker CPU 资源限制——CPU分片功能测试
    Centos7下用命令下载jdk7及jboss-eap-6
    Docker CPU 资源限制——CPU固定核功能测试
    更改MySQL数据文件目录位置
    Linux下资源利用率监测利器—nmon使用
    图示-Centos7完整安装
    Photoshop图层混合模式计算公式大全
    HMC5883L地磁传感器驱动
    ADXL345加速度传感器驱动
  • 原文地址:https://www.cnblogs.com/itachy/p/7197047.html
Copyright © 2011-2022 走看看