Java Thread模拟哲学家进餐

package thread1;

import java.util.ArrayList;

public class Dining {

    /** 
     * @Title: main 
     * @Description: TODO(这里用一句话描述这个方法的作用) 
     * @param @param args    设定文件 
     * @return void    返回类型 
     * @throws 
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Chopstick chopstick1 = new Chopstick("筷子 1 ");  
        Chopstick chopstick2 = new Chopstick("筷子 2 ");  
        Chopstick chopstick3 = new Chopstick("筷子 3 ");  
        Chopstick chopstick4 = new Chopstick("筷子 4 ");  
        Chopstick chopstick5 = new Chopstick("筷子 5 ");  
          
        Philosopherer philosopher1 = new Philosopherer("哲学家 1 ", chopstick5,chopstick1);  
        Philosopherer philosopher2 = new Philosopherer("哲学家 2 ", chopstick1,chopstick2);  
        Philosopherer philosopher3 = new Philosopherer("哲学家 3 ", chopstick2,chopstick3);  
        Philosopherer philosopher4 = new Philosopherer("哲学家 4 ", chopstick3,chopstick4);  
        Philosopherer philosopher5 = new Philosopherer("哲学家 5 ", chopstick4,chopstick5);  
        long startTime = System.currentTimeMillis();  
        ArrayList threads = new ArrayList();  
        threads.add(philosopher1);  
        threads.add(philosopher2);  
        threads.add(philosopher3);  
        threads.add(philosopher4);  
        threads.add(philosopher5);  
        //--开始进餐的顺序
        philosopher2.start();  
        philosopher4.start();  
        philosopher1.start();  
        philosopher3.start();  
        philosopher5.start();  
        for (Thread thread : threads) {  
            try {  
                thread.join();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
        System.out.println(System.currentTimeMillis() - startTime);
    }

}
class Chopstick {
    /**
     * 此筷子是否可以拿起
     */
    private boolean enable;
    
    /**
     * 此筷子的名称
     */
    public String name;
    
    public Chopstick(boolean enable, String name) {
        super();
        this.enable = enable;
        this.name = name;
    }
    
    public Chopstick(String name) {
        this(true,name);
    }
    
    public void setEnable(boolean enbl){
        this.enable = enbl;
    }
    
    public boolean getEnable(){
        return this.enable;
    }
    
    /**
     * 拿起筷子
     */
    public synchronized void pickup(){
        try{
            while(this.enable == false){
                wait();
            }
            this.enable = false;
        }catch(Exception e){
            
        }
    }
    
    /**
     * 放下手中的筷子
     */
    public synchronized void pickdown(){
        this.enable = true;
        this.notifyAll();
    }
}
class Philosopherer extends Thread{

    private String name;
    
    Chopstick leftChopstick;
    Chopstick rightChopstick;
    
    public Philosopherer(String name, Chopstick leftChopstick,
            Chopstick rightChopstick) {
        super();
        this.name = name;
        this.leftChopstick = leftChopstick;
        this.rightChopstick = rightChopstick;
    }

    @Override
    public void run() {
        super.run();
        leftChopstick.pickup();
        System.out.println(this.name+"拿起了左手的筷子"+leftChopstick.name);
        rightChopstick.pickup();
        System.out.println(this.name+"拿起了右手的筷子"+rightChopstick.name);
        System.out.println(this.name+"开始吃饭了");
        try {
            this.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(this.name+"吃饱了");
        rightChopstick.pickdown();
        System.out.println(this.name+"放下了右手的筷子"+this.rightChopstick.name);
        leftChopstick.pickdown();
        System.out.println(this.name+"放下了左手的筷子"+this.leftChopstick.name);
        
    }
}


转载于:https://my.oschina.net/sharkbobo/blog/270238

你可能感兴趣的:(Java Thread模拟哲学家进餐)