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();
                }
            }
        }
    }
  • 相关阅读:
    Two Sum
    Binary Tree Preorder Traversal *
    Rotate Array
    Repeated DNA Sequences
    Symmetric Tree
    Path Sum
    Python初学——多线程Threading
    Python初学——窗口视窗Tkinter
    pyinstaller打包多个py文件和去除cmd黑框
    python获取当前路径
  • 原文地址:https://www.cnblogs.com/TinyBobo/p/4735866.html
Copyright © 2011-2022 走看看