• 线程交互:生产消费模型


      这个例子利用线程的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()执行在后的情况发生

  • 相关阅读:
    使用 DataAdapter 执行批量更新 [摘自MSDN]
    深入浅出net泛型编程
    模版页中引用文件路径的问题
    查询SQLSERVER某个表所占用空间大小的SQL语句
    如何获取SQL Server数据库里表的占用容量大小的存储过程
    确定计算机是否可以运行 Windows Vista? 操作系统
    SQL语句 [转]
    SQLServer中如何将一个字段的多个记录值合在一行显示
    ASP.net在页面所有内容生成后、输出内容前对页面内容进行操作
    oracle 删除用于及其表空间
  • 原文地址:https://www.cnblogs.com/garfieldcgf/p/5519979.html
Copyright © 2020-2023  润新知