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

  • 相关阅读:
    NSURLConnection的异步请求方式
    <iOS>关于Xcode上的Other linker flags
    使用十六进制色值表示UIColor
    kubernetes & docker
    01 docker的安装与基本使用
    08 数组
    07 grep 正则
    06 信号处理和expect
    05 函数
    04 流程控制
  • 原文地址:https://www.cnblogs.com/garfieldcgf/p/5519979.html
Copyright © 2011-2022 走看看