zoukankan      html  css  js  c++  java
  • 生产者 和 消费者

    public class Meal {                
                                       
        private final int orderNum;    
                                       
        public Meal(int orderNum) {    
            this.orderNum = orderNum;  
        }                              
                                       
        public String toString() {     
            return "Meal" + orderNum;  
        }                              
    }                                  
    /**
     * 生产者
     */
    public class Producer implements Runnable {
    
        private Restaurant restaurant;
        private int count = 0;
    
        public Producer(Restaurant restaurant) {
            this.restaurant = restaurant;
        }
    
        @Override
        public void run() {
            try {
                while (!Thread.interrupted()) {
                    synchronized (this) {
                        while (restaurant.meal != null) {
                            wait();// 等消费
                        }
                    }
                    if (++count == 10) {
                        System.out.println("买完了");
                        restaurant.exec.shutdownNow();
                    }
                    System.out.println("订单来了!");
                    synchronized (restaurant.customer) {
                        restaurant.meal = new Meal(count);
                        System.out.println("做完了");
                        restaurant.customer.notifyAll();
                    }
    
                    TimeUnit.MILLISECONDS.sleep(100);
                }
    
    
            } catch (InterruptedException e) {
                System.out.println("消费者休息了!");
            }
    
        }
    
    }
    /**
     * 消费者
     */
    public class Customer implements Runnable {
    
        private Restaurant restaurant;
    
        public Customer(Restaurant restaurant) {
            this.restaurant = restaurant;
        }
    
        @Override
        public void run() {
            try {
                while (!Thread.interrupted()) {
                    synchronized (this) {
                        while (restaurant.meal == null) {
                            wait();// 等生产者生产
                        }
                    }
                    System.out.println("producer got " + restaurant.meal);
                    synchronized (restaurant.customer) {
                        restaurant.meal = null;
                        restaurant.customer.notifyAll();
                    }
    
                }
            } catch (InterruptedException e) {
                System.out.println("生产者休息了!");
            }
    
        }
    
    }
    public class Restaurant {
    
        Meal meal;
        ExecutorService exec = Executors.newCachedThreadPool();
    
        Producer producer = new Producer(this);
        Customer customer = new Customer(this);
    
        public Restaurant() {
            exec.execute(producer);
            exec.execute(customer);
        }
    
    }
    public class Main {
    
        public static void main(String[] args) {
            new Restaurant();
        }
    }
  • 相关阅读:
    shell 字符串切分成数组
    RDD关键性能考量之 内存管理
    RDD关键性能考量之 序列化格式
    【转】Linux ln(link) 命令详解
    【转】redis配置认证密码
    【转】MongoDB 知识要点一览
    RDD关键性能考量之 并行度
    使用thumbnailator不按照比例,改变图片的大小
    Linux服务器redhat配置本地yum源
    Oracle表字段的增删改和重命名
  • 原文地址:https://www.cnblogs.com/Mustr/p/7157919.html
Copyright © 2011-2022 走看看