小编msa*_*ogh的帖子

用餐Philosopher的解决方案最终陷入僵局

我已经为Dining Philosopher's Problem 实现了资源层次结构解决方案.当我尝试比较两个Chopsticks的n值时,它们最终陷入僵局.但是,如果我使用他们的hashCodes而不是n值,它会顺利运行.为何如此区别?这一天结束时他们都不是?

import java.util.Random;

class Chopstick {
    public final int n;
    public Chopstick(int n) {
        this.n = n;
    }
}

class Philosopher extends Thread {
    private Chopstick left, right;
    private Random random;
    private final int n;

    public Philosopher(int n, Chopstick left, Chopstick right) {
        this.n = n;
        if (left.n > right.n) { // no deadlock if I replace this with left.hashCode() > right.hashCode()
            this.right = left;
            this.left = right;
        } else {
            this.left = left;
            this.right …
Run Code Online (Sandbox Code Playgroud)

java multithreading dining-philosopher

1
推荐指数
1
解决办法
398
查看次数

标签 统计

dining-philosopher ×1

java ×1

multithreading ×1