-5

我今天有一个学校作业来模拟哲学家进餐问题。

我刚刚编写了这段代码来测试它是否以这种简单的方式工作(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();

    }

}
4

1 回答 1

0

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

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

chopstick3 = false;

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

您必须同步对筷子变量的访问和修改。

于 2015-11-30T19:24:59.353 回答