Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 爪哇餐饮哲学家_Java_Multithreading_Dining Philosopher - Fatal编程技术网

Java 爪哇餐饮哲学家

Java 爪哇餐饮哲学家,java,multithreading,dining-philosopher,Java,Multithreading,Dining Philosopher,我今天收到一份学校作业,用来模拟进餐哲学家的问题 我只是做了这个代码来测试它是否以这种简单的方式工作(A)。 我现在遇到的问题是,当一个不应该吃东西的人开始吃东西的时候。这意味着当筷子被拿走时,他无论如何都会开始吃东西。。。请给我提示:)以下是代码: import java.util.*; public class OperatingSystem implements Runnable { int namn; // thread name int tal;

我今天收到一份学校作业,用来模拟进餐哲学家的问题

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

    }

}

这是典型的数据竞争问题。同步是关键

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

chopstick3 = false;
由于条件
((chopstick4==true)和&(chopstick5==true))
为true,因此没有任何东西会阻止namn=4输入if。(Namn 3尚未执行
chopptick4=false;


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

我想您所谓的“简易方法”一点也不容易。您可能需要重新考虑您的方法