今天,我决定尝试解决餐饮哲学家的问题。所以我写下面的代码。但是我认为这是不正确的,因此如果有人告诉我这有什么问题,我将感到高兴。我使用forks来锁定(我只读它们,因为我不把对它们的访问放在同步块中),我有扩展线程的类,并且它保留了两个锁。

import java.util.Random;

public class EatingPhilosophersProblem {

private final static Random RANDOM = new Random();

/**
 *
 * @author Damyan Class represents eating of every philosopher. It
 *         represents infinity cycle of eating.
 */
private static class PhilosopherEating extends Thread {

    int forkOne;
    int forkTwo;

    public PhilosopherEating(String name, int forkOne, int forkTwo) {
        super(name);
        this.forkOne = forkOne;
        this.forkTwo = forkTwo;
    }

    @Override
    public void run() {
        super.run();

        while (true) {
            requireLock(this, forkOne, forkTwo);
        }
    }

}

private static Boolean[] forks = new Boolean[] { new Boolean(true), new Boolean(true), new Boolean(true),
        new Boolean(true), new Boolean(true) };
// locks should be created by new, otherwise almost 100% sure that they will
// point to the same object (because of java pools)
// this pools are used from java for immutable objects

private static void requireLock(PhilosopherEating philosopherEating, int firstIndex, int secondIndex) {

    // we lock always from the the lower index to the higher, otherwise
    // every philosopher can take his left fork and deadlock will apear

    if (firstIndex > secondIndex) {
        int temp = firstIndex;
        firstIndex = secondIndex;
        secondIndex = temp;
    }

    if (firstIndex == 4 || secondIndex == 4) {
        System.err.println(firstIndex + " and " + secondIndex);
    }

    synchronized (forks[firstIndex]) {
        synchronized (forks[secondIndex]) {

            printPhilosopherhAction(philosopherEating, "start eating");

            try {
                Thread.sleep(RANDOM.nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            printPhilosopherhAction(philosopherEating, "stop eating");

        }
    }
};

private static void printPhilosopherhAction(PhilosopherEating philosopherEating, String action) {
    System.out.println("Philosopher " + philosopherEating.getName() + " " + action);

}

public static void main(String[] args) {

    PhilosopherEating first = new PhilosopherEating("1 - first", 0, 1);
    PhilosopherEating second = new PhilosopherEating("2 - second", 1, 2);
    PhilosopherEating third = new PhilosopherEating("3 - third", 2, 3);
    PhilosopherEating fourth = new PhilosopherEating("4 - fourth", 3, 4);
    PhilosopherEating fifth = new PhilosopherEating("5 - fifth", 4, 0);

    first.start();
    second.start();
    third.start();
    fourth.start();
    fifth.start();

}


我认为这是有问题的,因为第五个哲学家从未进食,而第四和第三位哲学家则大多在进食。
提前致谢。

最佳答案

您的问题有一个名字:它被称为线程“饥饿”。当多个线程争用同一资源,并且其中一个(或多个)被连续拒绝访问时,就会发生这种情况。

弄清楚如何避免僵局是用餐哲学家之谜的一个方面,但是弄清楚如何让每个哲学家获得充足的进餐时间可能是另一方面。

JP Moresmau的答案建议您强迫每个哲学家休息一下(在经典难题中通常被称为“思考”),以便其他哲学家转而吃东西。那是可行的,但是如果您将哲学家视为某个应用程序中的工作线程,则“进食”对应于工作线程正在做一些有用的工作,而“休息”或“思考”则对应于处于空闲状态的线程,这可能就是您所做的事情希望避免。

如果所有的哲学家总是饿着,那么要确保所有的哲学家都能公平地享用进餐时间,不仅需要锁。

这是一个提示:可以保证任何形式的“公平”的高级同步对象通常在实现中使用队列。

10-08 12:40