Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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 - Fatal编程技术网

Java 方法在我不希望的地方被调用

Java 方法在我不希望的地方被调用,java,multithreading,Java,Multithreading,我有一个著名哲学家问题的多线程代码示例。在这个场景中,5位哲学家试图吃饭,但他们与坐在他们旁边的下一位哲学家共用同一把叉子 我在理解此代码中线程的工作方式时遇到问题: 当我观察这段代码的输出时,它看起来像是皮卡在等待之后执行测试。但考虑到测试在等待之前就已经明确了,这怎么可能呢 示例输出: 哲学0是饥饿的 哲学0正在吃东西 哲学家:0。 哲学3饿了 哲学3正在吃东西 哲学家饮食:0.3。 哲学1是饥饿的 哲学2饿了 哲学4是饥饿的 哲学0在思考 哲学1正在吃东西 Philosoph1正在吃由te

我有一个著名哲学家问题的多线程代码示例。在这个场景中,5位哲学家试图吃饭,但他们与坐在他们旁边的下一位哲学家共用同一把叉子

我在理解此代码中线程的工作方式时遇到问题:

当我观察这段代码的输出时,它看起来像是皮卡在等待之后执行测试。但考虑到测试在等待之前就已经明确了,这怎么可能呢

示例输出:

哲学0是饥饿的 哲学0正在吃东西 哲学家:0。 哲学3饿了 哲学3正在吃东西 哲学家饮食:0.3。 哲学1是饥饿的 哲学2饿了 哲学4是饥饿的 哲学0在思考 哲学1正在吃东西

Philosoph1正在吃由test1打印,这被称为pickup1,但这怎么可能呢

阶级哲学:

班上餐厅:

你们错过了那个测试,它不仅由拾音器调用,也由放下调用

Philosoph 1正在吞噬示例中的输出不是由pickup1调用的,而是由putdown0调用的。放下武器需要对哲学家进行前后的考验

表达式i+4%5和i+1%5可能有点混淆,但i+4%5给出了前面的哲学家:

0+4%5=4 1 + 4 % 5 = 0 2 + 4 % 5 = 1 3 + 4 % 5 = 2 4+4%5=3

i+1%5给出了下一个哲学家:

0+1%5=1 1 + 1 % 5 = 2 2 + 1 % 5 = 3 3 + 1 % 5 = 4 4+1%5=0

class Philosoph extends Thread {
    Dining_Philosophers dp;
    int name;

    public Philosoph(int n, Dining_Philosophers d) {
        name = n;
        dp = d;
    }

    public void run() {
        while (true) {
            thinking();
            dp.pickup(name);
            eating();
            dp.putdown(name);
        }
    }

    public static void main(String[] args) {
        Dining_Philosophers dp = new Dining_Philosophers();
        Philosoph p0 = new Philosoph(0, dp);
        Philosoph p1 = new Philosoph(1, dp);
        Philosoph p2 = new Philosoph(2, dp);
        Philosoph p3 = new Philosoph(3, dp);
        Philosoph p4 = new Philosoph(4, dp);
        p0.start();
        p1.start();
        p2.start();
        p3.start();
        p4.start();
    }

    void thinking() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {}
    }

    void eating() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {}
    }
}
class Dining_Philosophers {
    static int thinking = 0;
    static int hungry = 1;
    static int eating = 2;
    int[] state = new int[5];

    public Dining_Philosophers() {
        for (int i = 0; i < 5; i++) {
            state[i] = thinking;
        }
    }

    public synchronized void pickup(int i) {
        //The Thread executes this function, but 
        // when it executes the wait function und wake up after the notification by another
        // thread then it executes only the test-function. But why only the it and not the  
        // other Code like the "System.out.println("Philosoph " + i + " is hungry");" ?
        state[i] = hungry;
        System.out.println("Philosoph " + i + " is hungry");
        test(i);
        while (state[i] != eating) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
    }

    public synchronized void putdown(int i) {
        state[i] = thinking;
        System.out.println("Philosoph " + i + " is thinking");
        test((i + 4) % 5);
        test((i + 1) % 5);
    }

    public void test(int k) {
        int i;
        if ((state[(k + 4) % 5] != eating) && (state[k] == hungry) && (state[(k + 1) % 5] != eating)) {
            state[k] = eating;
            System.out.println("Philosoph " + k + " is eating");
            System.out.print("Philosophers eating: ");
            for (i = 0; i < 5; i++)
                if (state[i] == eating)
                    System.out.print(i + " ");
            System.out.println(".");
            notifyAll();
        }
    }
}