zoukankan      html  css  js  c++  java
  • [JAVA 多线程] 生产者消费者实例

    正好有人问,就直接将代码记录下来。

    背景:有一个仓库存储货物,存在着生产者和消费者,设计一个可以并发的实现。

    设计思路:设计一个仓库类,类中保存最大的容量限制和当前的count,类中包含生产和消费的方法,并且都是synchronized。

    具体代码:

    package com.test.tiny;
    
    public class Store {
        private final int MAX_SIZE; //最大
        private int count; // 当前
        
        public Store(int n) {
            MAX_SIZE = n;
            count = 0;
        }
        
        public synchronized void add() {
            while(count >= MAX_SIZE) {
                System.out.println("已经满了");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            count++;
            System.out.println(Thread.currentThread().toString() + " put :" + count);
            this.notifyAll();
        }
        
        public synchronized void remove() {
            while(count <= 0) {
                System.out.println("已经空了");
                try {
                    this.wait();
                }catch(Exception e) {
                    e.printStackTrace();
                }
            }
            
            System.out.println(Thread.currentThread().toString() + " get :" + count);
            count--;
            this.notifyAll();
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Store s = new Store(5);
            Thread pro = new Producer(s);
            Thread con = new Consumer(s);
            Thread pro2 = new Producer(s);
            Thread con2 = new Consumer(s);
            
            pro.setName("生产者1");
            con.setName("消费者1");
            pro2.setName("生产者2");
            con2.setName("消费者2");
            
            pro.start();
            con.start();
            pro2.start();
            con2.start();
        }
    }
    class Producer extends Thread {
        private Store s;
        public Producer(Store s) {
            this.s = s;
        }
        public void run() {
            while(true) {
                s.add();
                try {
                    Thread.sleep(1000);  // 为了直观,休息1秒
                }catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    class Consumer extends Thread {
        private Store s;
        
        public Consumer(Store s) {
            this.s = s;
        }
        
        public void run() {
            while(true) {
                s.remove();
                try {
                    Thread.sleep(1500); // 为了直观 多休息0.5秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    Docker核心技术之镜像(8)
    简单的自定义函数(7)
    存储过程游标的使用(6)
    存储过程循环语句(5)
    存储过程条件语句(4)
    存储过程的参数(3)
    存储过程的变量(2)
    一个简单的存储过程(1)
    Docker加速器配置(7)
    单表、多表查询
  • 原文地址:https://www.cnblogs.com/TinyBobo/p/4735866.html
Copyright © 2011-2022 走看看