• 生产者与消费者——厨师和消费者之间的问题


    厨师作为生产者,每做一道菜,消费者就消灭一道菜;dish作为一个共有属性

    public class Cooker implements Runnable {
      private String name;
      private Dish d;
      private String food;
      public String [] foods = new String [] {"红烧鲫鱼","红烧肉","小炒牛肉","小白菜","番茄炒蛋","火锅","米饭"};

      public Cooker(String name, Dish d) {
        this.d = d;
        this.name = name;
      }

      public void run() {
        cook();
      }

      public void cook() {
        synchronized(d) {
          for(int i = 0; i < 10; i++) {
            Random random = new Random();
            food = foods[random.nextInt(foods.length)];
            d.setFood(food);
            if(d.isEmpty()) { //d.isEmpty为true,即盘子为空,则进入
              d.setEmpty(false);
              System.out.println(name + "烹饪了美食:" + food);
              d.notify();
              try {
                d.wait();
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
            }else {
              try {
                d.wait();
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
            }
          }
        }
      }
    }

    public class Dish {
      private String food;
      private boolean isEmpty = true;

      public String getFood() {
        return food;
      }

      public void setFood(String food) {
        this.food = food;
      }

      public boolean isEmpty() {
        return isEmpty;
      }

      public void setEmpty(boolean isEmpty) {
        this.isEmpty = isEmpty;
      }
    }

    public class Customer implements Runnable {
      private String name;
      private Dish d;

      public Customer(String name, Dish d) {
        this.name = name;
        this.d = d;
      }

      public void run() {
        eat();
      }

      public void eat() {
        synchronized(d) {
          while(true) {
            if(!d.isEmpty()) {      //d.isEmpty为false,即盘子不为空,则进入
              d.setEmpty(true);
              System.out.println(name + "享用了美食:" + d.getFood());
              d.notify();
              try {
                d.wait();
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
            }else {
              try {
                d.wait();
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
            }
          }
        }
      }

      public Dish getD() {
        return d;
      }

      public void setD(Dish d) {
        this.d = d;
      }
    }

    public class TestProducerAndCustomer {
      public static void main(String [] args) {
        Dish d = new Dish();
        Cooker ck = new Cooker("老王",d);
        Customer cm = new Customer("小太阳",d);

        Thread tck = new Thread(ck);
        Thread tcm = new Thread(cm);

        tck.start();
        tcm.setDaemon(true);
        tcm.start();
      }
    }

  • 相关阅读:
    Appium运行时,error: Logcat capture failed: spawn ENOENT的解决办法
    pwntools使用简介3
    pwntools使用简介2
    pwnable.kr memcpy之write up
    pwnable.kr uaf之wp
    【笔记】objdump命令的使用
    pwnable.kr cmd2之write up
    pwnable.kr cmd1之write up
    pwnable.kr lotto之write up
    pwnable.kr blackjack之write up
  • 原文地址:https://www.cnblogs.com/helloworldlx/p/8536928.html
Copyright © 2020-2023  润新知