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

    class Meal {
    private final int orderNum;
    public Meal(int orderNum) { this.orderNum = orderNum;}
    public String toString() { return "Meal " + orderNum; }
    }

    class WaitPerson implements Runnable {
    private Restaurant restaurant;
    public WaitPerson(Restaurant r) { restaurant = r; }
    public void run() {
    try {
    while(!Thread.interrupted()) {
    synchronized(this) {
    while(restaurant.meal == null)
    wait(); // ... for the chef to produce a meal
    }
    print("Waitperson got " + restaurant.meal);
    synchronized(restaurant.chef) {
    restaurant.meal = null;
    restaurant.chef.notifyAll(); // Ready for another
    }
    }
    } catch(InterruptedException e) {
    print("WaitPerson interrupted");
    }
    }
    }

    class Chef implements Runnable {
    private Restaurant restaurant;
    private int count = 0;
    public Chef(Restaurant r) { restaurant = r; }
    public void run() {
    try {
    while(!Thread.interrupted()) {
    synchronized(this) {
    while(restaurant.meal != null)
    wait(); // ... for the meal to be taken
    }
    if(++count == 10) {
    print("Out of food, closing");
    restaurant.exec.shutdownNow();
    }
    printnb("Order up! ");
    synchronized(restaurant.waitPerson) {
    restaurant.meal = new Meal(count);
    restaurant.waitPerson.notifyAll();
    }
    TimeUnit.MILLISECONDS.sleep(100);
    }
    } catch(InterruptedException e) {
    print("Chef interrupted");
    }
    }
    }

    public class Restaurant {
    Meal meal;
    ExecutorService exec = Executors.newCachedThreadPool();
    WaitPerson waitPerson = new WaitPerson(this);
    Chef chef = new Chef(this);
    public Restaurant() {
    exec.execute(chef);
    exec.execute(waitPerson);
    }
    public static void main(String[] args) {
    new Restaurant();
    }
    } /* Output:
    Order up! Waitperson got Meal 1
    Order up! Waitperson got Meal 2
    Order up! Waitperson got Meal 3
    Order up! Waitperson got Meal 4
    Order up! Waitperson got Meal 5
    Order up! Waitperson got Meal 6
    Order up! Waitperson got Meal 7
    Order up! Waitperson got Meal 8
    Order up! Waitperson got Meal 9
    Out of food, closing
    WaitPerson interrupted
    Order up! Chef interrupted
    *///:~

    or:

    package producer;

    class Meal
    {
    private int orderNo;

    public Meal(int orderNo) {
    this.orderNo = orderNo;
    }

    @Override
    public String toString() {
    return "Meal [orderNo=" + orderNo + "]";
    }
    }
    class Resource
    {
    Meal meal;
    Producer pro;
    Customer customer;

    public Meal getMeal() {
    return meal;
    }

    public void setMeal(Meal meal) {
    this.meal = meal;
    }

    public Resource()
    {
    }
    }


    public class TestProducer {

    public static void main(String[] args) throws InterruptedException {
    Resource res = new Resource();
    Producer p = new Producer(res);
    Customer c = new Customer(res);
    Thread t1 = new Thread(p);
    Thread t3 = new Thread(c);
    t1.start();
    t3.start();
    Thread.currentThread().sleep(100000);
    }
    }


    class Producer implements Runnable
    {
    public Resource res;
    static int count;

    public Resource getRes()
    {
    return res;
    }

    public void setRes(Resource res)
    {
    this.res = res;
    }

    public Producer(Resource res) {
    this.res = res;
    res.pro=this;
    }

    @Override
    public void run()
    {
    while(true)
    {
    synchronized (this)
    {
    while(res.getMeal() != null)
    {
    try {
    wait();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    System.out.println("begin to proceduce: "+ Thread.currentThread() + count);
    synchronized (res.customer) {
    res.setMeal(new Meal(count++));
    res.customer.notifyAll();
    }
    }

    }

    }

    class Customer implements Runnable
    {
    public Resource res;

    public Resource getRes() {
    return res;
    }

    public void setRes(Resource res) {
    this.res = res;
    }

    public Customer(Resource res) {
    this.res = res;
    res.customer= this;
    }

    @Override
    public void run()
    {
    while(true)
    {
    synchronized (this)
    {
    while (res.meal == null)
    {
    try {
    wait();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    System.out.println("customer got meal: "+res.meal.toString());
    synchronized (res.pro)
    {
    res.meal = null;
    res.pro.notifyAll();
    }
    }
    }
    }

  • 相关阅读:
    NTC温度采集之数据拟合——freemat软件实现
    头文件中不能定义变量
    好的博客空间收藏
    STM32F407VET6之IAR之ewarm7.80.4工程建立(基于官方固件库1.6版本) 的工程文件目录
    STM32F407VET6之IAR之ewarm7.80.4工程建立(基于官方固件库1.6版本)
    JAVA反射机制o
    Java反射机制
    c+内存管理机制
    java内存空间详解
    JAVA内存管理再解
  • 原文地址:https://www.cnblogs.com/daxiong225/p/9074249.html
Copyright © 2011-2022 走看看