zoukankan      html  css  js  c++  java
  • Java

    1、wait()惯用法:wait()包装在一个while语句中,因为某个其他任务可能会在WaitPerson被唤醒时,会突然插足并拿走订单;

    2、只能在同步控制方法或同步控制块里调用wait()、notify()和notifyAll();

    import java.util.concurrent.*;
    
    public class Meal {
            private final int orderNum;
            public Meal(int orderNum){
                this.orderNum=orderNum;
            }
            public String toString(){
                return "Meal "+orderNum;
            }
    }
    
    public class Restaurant {
        Meal meal;
        ExecutorService exec=Executors.newCachedThreadPool();
        WaitPerson waitPerson=new WaitPerson(this);
        Chef chef=new Chef(this);
        
        public Restaurant(){
            exec.execute(waitPerson);
            exec.execute(chef);
        }
    }
    
    public class WaitPerson implements Runnable{
    
        private Restaurant restaurant;
        public  WaitPerson(Restaurant r) {
            restaurant=r;
        }
        
        @Override
        public void run() {
            try{
                while(!Thread.interrupted()){
                    synchronized (this) {
                        while(restaurant.meal==null){
                            wait(); // for the chef to produce a meal
                        }
                    }
                    System.out.println("WaitPerson got "+restaurant.meal);
                    
                    synchronized(restaurant.chef){
                        restaurant.meal=null;
                        restaurant.chef.notifyAll(); 
                    }
                }
            }catch(InterruptedException e){
                System.out.println("WaitPerson interrupted");
            }    
        }
    }
    
    public class Chef implements Runnable {
    
        private Restaurant restaurant;
        private int count=0;
        public Chef(Restaurant r){
            restaurant=r;
        }
        
        @Override
        public void run() {
            try{
                while(!Thread.interrupted()){
                    synchronized(this){
                        while(restaurant.meal!=null){
                            wait();
                        }
                    }
                    
                    if(++count==10){
                        System.out.println("Out of food.closing");
                        restaurant.exec.shutdownNow();
                    }
                    
                    System.out.print("Order up! ");
                    
                    synchronized(restaurant.waitPerson){
                        restaurant.meal=new Meal(count);
                        restaurant.waitPerson.notifyAll();
                    }
                    
                    //TimeUnit.MILLISECONDS.sleep(100);
                }
            }catch(InterruptedException e){
                System.out.println("Chef interrupted");
            }
        }
    }
  • 相关阅读:
    mysql主从复制+读写分离
    lnmp平台菜鸟入门级笔记
    Shell编程
    Shell编程之--“grep-awk-sed” 基础用法汇总
    Nginx
    LB负载均衡之Nginx-Proxy
    LB(Load balance)负载均衡集群-LVS
    HA(High available)-Keepalived高可用性集群(双机热备)单点实验
    HA(High available)--Heartbeat高可用性集群(双机热备)
    linux -小记(3) 问题:linux 安装epel扩展源报错
  • 原文地址:https://www.cnblogs.com/null2/p/5089219.html
Copyright © 2011-2022 走看看