在我不期望的地方调用方法

时间:2019-09-29 16:32:46

标签: java multithreading

我有一个众所周知的Philosopher问题的多线程代码示例。在这种情况下,有5位哲学家尝试进餐,但他们与旁边的下一位哲学家共享同一叉子。

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

当我观察到这段代码的输出时,看起来pickuptest之后执行了wait。但是,考虑到test显然早于wait,这怎么可能?

示例输出:

  

哲学0饿了
  哲学0在吃东西
  哲学家在吃饭:0。
  哲学3饿了
  哲学3在吃东西
  哲学家在吃饭:0 3。
  哲学1饿了
  哲学2饿了
  哲学4饿了
  哲学0在思考
  哲学1在吃饭

{Phosophoph 1正在吃东西”由test(1)打印,并由pickup(1)调用,但这怎么可能呢?

课堂哲学:

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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您不知道test不仅会被pickup调用,还会被putdown调用。

您的示例中的“哲学1正在吃东西” 输出不是pickup(1)调用的,而是putdown(0)调用的。 putdown前后都会向哲学家打电话test

表达式(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