zoukankan      html  css  js  c++  java
  • 线程交互:生产消费模型

      这个例子利用线程的wait(),notify(),以及同步和锁来实现,主要为了加深方法和交互理解,简单介绍:

      1.仓储初始100

      2.随机生产或消费,大于90时不生产,小于20时不消费

      3.无限运行

    package timeInterval;
    
    public class InitNum {
        public static int num = 100;
        public static int timeInterval = 5000;
    
        public static void main(String[] args) throws InterruptedException {
            System.out.println("init num : " + num);
            while (true) {
                System.out.println("current num : " + num);
                if (Math.random() > 0.5) {
                    System.out.println("begin to produce...");
                    Producer producer = new Producer();
                    Thread t = new Thread(producer);
                    t.start();
                    synchronized (t) {
                        System.out.println("producing...");
                        t.wait();
                        System.out.println("stop produce");
                    }
                } else {
                    System.out.println("begin to consume...");
                    Customer customer = new Customer();
                    Thread t2 = new Thread(customer);
                    t2.start();
                    synchronized (t2) {
                        System.out.println("consuming...");
                        t2.wait();
                        System.out.println("stop consume");
                    }
                }
            }
    
        }
    }
    
    class Producer implements Runnable {
    
        @Override
        public void run() {
            synchronized (this) {
                if (InitNum.num < 90) {
                    double d = Math.ceil(Math.random() * 10);
                    InitNum.num += d;
                    System.out.println("produce num + : " + d);
                } else {
                    System.out.println("cannot produce...");
                }
                try {
                    Thread.sleep(InitNum.timeInterval);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("go on...");
                notify();
            }
    
        }
    
    }
    
    class Customer implements Runnable {
    
        @Override
        public void run() {
            synchronized (this) {
                if (InitNum.num > 20) {
                    double d = Math.ceil(Math.random() * 20);
                    InitNum.num -= d;
                    System.out.println("consume num - : " + d);
                } else {
                    System.out.println("cannot consume...");
                }
                try {
                    Thread.sleep(InitNum.timeInterval);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("go on...");
                notify();
            }
        }
    
    }

      运行结果:

    init num : 100
    current num : 100
    begin to produce...
    producing...
    cannot produce...
    go on...
    stop produce
    current num : 100
    begin to produce...
    producing...
    cannot produce...
    go on...
    stop produce
    current num : 100
    begin to consume...
    consuming...
    consume num - : 9.0
    go on...
    stop consume
    current num : 91
    begin to consume...
    consuming...
    consume num - : 20.0
    go on...
    stop consume
    current num : 71
    begin to consume...
    consuming...
    consume num - : 5.0
    go on...
    stop consume
    current num : 66
    begin to produce...
    producing...
    produce num + : 6.0

      生产时间定为5s,避免notify() 执行在前,wait()执行在后的情况发生

  • 相关阅读:
    macOS 常用配置
    maven 常用配置
    log4j(1.x)最全配置!!!
    Python2 中 input() 和 raw_input() 的区别
    用 Maven 构建 Java-Scala 混合项目
    trim() 的秘密
    【目录】一起来学 Kafka 吧
    Spring in Action.4th
    Hibernate快速入门
    html2canvas 跨域图片无法正常加载问题解决办法
  • 原文地址:https://www.cnblogs.com/garfieldcgf/p/5519979.html
Copyright © 2011-2022 走看看