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()执行在后的情况发生

  • 相关阅读:
    遍历一个枚举类型
    ASP.NET:C#中时间格式的转换
    DataAdapter去批量更新数据的FAQ
    .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (第2版) (C# DIY HttpWebClient) 收藏
    如何使数据库中取出的数据保持原有格式
    如何获取控制台应用程序自己的文件名
    2008将倒掉一大部分的工厂和贸易公司
    组六对半分组组合投资方案(36789)
    重又归孑然一身
    善于总结
  • 原文地址:https://www.cnblogs.com/garfieldcgf/p/5519979.html
Copyright © 2011-2022 走看看