餐饮哲学家Java

时间:2015-11-30 19:13:54

标签: java multithreading dining-philosopher

我今天得到了一份学校作业,以模拟餐饮哲学家的问题。

我刚刚制作了这段代码来测试它是否以这种简单方式工作(A)。 我现在遇到的问题是,当一个不应该吃东西的人开始吃东西时。意思是当筷子被拿走时他开始吃了...请给我提示:)这里是代码:

import java.util.*;
public class OperatingSystem implements Runnable {

    int namn;       // thread name
    int tal;        // the random number
    Random rand = new Random(); // implements random
    boolean chopstick1 = true;
    boolean chopstick2 = true;
    boolean chopstick3 = true;
    boolean chopstick4 = true;
    boolean chopstick5 = true;

    public OperatingSystem(int x){      // constructor
        namn = x;
        tal = rand.nextInt(30000);
    }



    public void think() {       // run method
    try{
        System.out.println(namn + ": " + tal+ " ms");
        Thread.sleep(tal);
        System.out.println(namn + " is done thinking!");

    }catch(Exception e){}

    }
    public void hungry(){

        while(true){
        //  System.out.println(namn);
        if(namn == 1){
                if((chopstick1==true) && (chopstick2==true)){
                    chopstick1 = false;
                    chopstick2 = false;
                    break;
            }


        }
        else if(namn == 2){
                if((chopstick2==true) && (chopstick3==true)){
                    chopstick2 = false;
                    chopstick3 = false;
                    break;
                }   


        }
        else if(namn == 3){
                if((chopstick3==true) && (chopstick4==true)){
                    chopstick3 = false;
                    chopstick4 = false;
                    break;
            }

        }
        else if(namn == 4){
            if((chopstick4==true) && (chopstick5==true)){
                chopstick4 = false;
                chopstick5 = false;
                break;


            }

        }
        else if(namn == 5){
            if((chopstick5==true) && (chopstick1==true)){
                chopstick5 = false;
                chopstick1 = false;
                break;
            }

            }
        }


    }
    public void eat() {     // run method
        try{
            tal = rand.nextInt(30000);
            System.out.println(namn + " is eating");
            Thread.sleep(tal);
            System.out.println(namn + " is done eating!");
        /*  chopstick2 = true;
            chopstick3 = true;
            chopstick4 = true;
            chopstick5 = true;
            chopstick1 = true;
            */
            if(namn == 1){
                    chopstick1 = true;
                    chopstick2 = true;
            }
            else if(namn == 2){
                    chopstick2 = true;
                    chopstick3 = true;

                }


            else if(namn == 3){
                    chopstick3 = true;
                    chopstick4 = true;

                }


            else if(namn == 4){
                    chopstick4 = true;
                    chopstick5 = true;

                }


            else if(namn == 5){
                    chopstick5 = true;
                    chopstick1 = true;

                }


        }catch(Exception e){}

        }


    @Override
    public void run() {
        think();
        hungry();
        eat();
    }



}

///////////////////////////////// here comes the main;


import java.util.*;
public class OperatingSystemMain {

    public static void main(String[] args) {
        Thread t1 = new Thread(new OperatingSystem(1));
        Thread t2 = new Thread(new OperatingSystem(2));
        Thread t3 = new Thread(new OperatingSystem(3));
        Thread t4 = new Thread(new OperatingSystem(4));
        Thread t5 = new Thread(new OperatingSystem(5));

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();

    }

}

1 个答案:

答案 0 :(得分:0)

您遇到了经典的数据竞争问题。同步它是关键。

例如,如果namn = 3并且当前正在运行

chopstick3 = false;

没有什么可以阻止namn = 4输入if,因为条件((chopstick4==true) && (chopstick5==true))为真。 (Namn 3尚未执行chopstick4 = false;

您必须将访问和修改同步到筷子变量。