线程交互:生产消费模型

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

posted @ 2016-05-23 14:56  但行好事-莫问前程  阅读(284)  评论(0编辑  收藏  举报