0

我已经使用 Java 中的 Monitor (Synchronized) 实现了餐饮哲学家问题。

该计划的目标是:

  • 每个哲学家都应该遵循思考、拿筷子、吃饭、放筷子的工作流程(没有比赛条件)。

  • 无死锁

我认为这段代码似乎工作正常,但有些地方不对,因为它永远运行我试图调试它,调试工具停在这一行哲学家[i].t.join(); 但该程序并未终止。

请帮助我确定问题或告诉我如何解决它。感谢您的意见。

MyMonitor 类:

class MyMonitor {
    private enum States {THINKING, HUNGRY, EATING};
    private States[] state;

    public MyMonitor() {
        state = new States[5];
        for(int i = 0; i < 5; i++) {
            state[i] = States.THINKING;
            System.out.println("Philosopher " + i + " is THINKING");
        }
    }

    private void test(int i) {
        if((state[(i+4)%5]!=States.EATING) && (state[i]==States.HUNGRY) && (state[(i+1)%5]!=States.EATING)) {
            state[i] = States.EATING;
            System.out.println("Philosopher " + i + " is EATING");
            notify();
        }
    }

    public synchronized void pickup(int i) {
            state[i] = States.HUNGRY;
            System.out.println("Philosopher " + i + " is HUNGRY");      
            test(i);
            if (state[i] != States.EATING) {
                System.out.println("Philosopher " + i + " is WAITING");
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    }

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

我的哲学家类:

class MyPhilosopher implements Runnable{
    private int myID;
    private int eatNum;
    private MyMonitor monitor;
    private Thread t;

    public MyPhilosopher(int myID, int eatNum, MyMonitor monitor) {
        this.myID = myID;
        this.eatNum = eatNum;
        this.monitor = monitor;
        t = new Thread(this);
        t.start();
    }

    public void run() {
        int count = 1;
        while(count <= eatNum ){
            monitor.pickup(myID);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            monitor.putdown(myID);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            count++;
        }
    }

    public static void main(String[] args) {
        int eatNum = 10;

            System.out.println("----------------------------------------------------------------------------------------------------");
        System.out.println("xxx");
        System.out.println("xxx");
        System.out.println("xxx");
            System.out.println("----------------------------------------------------------------------------------------------------");
        System.out.println("Starting");
        System.out.println("----------------------------------------------------------------------------------------------------");
        System.out.println("");
        MyMonitor monitor = new MyMonitor();
        MyPhilosopher[] philosopher = new MyPhilosopher[5];

        for(int i = 0; i < 5; i++) {
            philosopher[i] = new MyPhilosopher(i, eatNum, monitor);
        }


        for(int i = 0; i < 5; i++) {
            try {
                philosopher[i].t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

            System.out.println("----------------------------------------------------------------------------------------------------");
        System.out.println("Ended");
    }
}
4

1 回答 1

0

我已经执行了您的代码,并且它运行完美,直到执行两次或多次。此外,您可以减少睡眠时间,您的代码是正确但完美的,直到 4 个 pilosopher 正在等待并且其中一个正在吃饭。我不喜欢它。你打破了一个科夫曼条件,但我建议你使用其他实现,比如打破保持和等待条件。我的意思是,你可以两根筷子都拿,也可以不拿,其他的实现可以是,偶数的 pilosophers 拿右边的筷子,奇数的 pilosophers 拿左边的筷子。祝你好运!

Philosopher 4 is THINKING
Philosopher 0 is EATING
Philosopher 3 is HUNGRY
Philosopher 3 is WAITING
Philosopher 1 is HUNGRY
Philosopher 1 is WAITING
Philosopher 2 is THINKING
Philosopher 3 is EATING
Philosopher 0 is THINKING
Philosopher 1 is EATING
Philosopher 4 is HUNGRY
Philosopher 4 is WAITING
Philosopher 3 is THINKING
Philosopher 4 is EATING
Philosopher 2 is HUNGRY
Philosopher 2 is WAITING
Philosopher 0 is HUNGRY
Philosopher 0 is WAITING
Philosopher 1 is THINKING
Philosopher 2 is EATING
Philosopher 4 is THINKING
Philosopher 0 is EATING
Philosopher 3 is HUNGRY
Philosopher 3 is WAITING
Philosopher 2 is THINKING
Philosopher 3 is EATING
Philosopher 1 is HUNGRY
Philosopher 1 is WAITING
Philosopher 0 is THINKING
Philosopher 1 is EATING
Philosopher 4 is HUNGRY
Philosopher 4 is WAITING
Philosopher 3 is THINKING
Philosopher 4 is EATING
Philosopher 2 is HUNGRY
Philosopher 2 is WAITING
Philosopher 0 is HUNGRY
Philosopher 0 is WAITING
Philosopher 1 is THINKING
Philosopher 2 is EATING
Philosopher 3 is HUNGRY
Philosopher 3 is WAITING
Philosopher 4 is THINKING
Philosopher 0 is EATING
Philosopher 2 is THINKING
Philosopher 3 is EATING
Philosopher 1 is HUNGRY
Philosopher 1 is WAITING
Philosopher 4 is HUNGRY
Philosopher 4 is WAITING
Philosopher 0 is THINKING
Philosopher 1 is EATING
Philosopher 2 is HUNGRY
Philosopher 2 is WAITING
Philosopher 3 is THINKING
Philosopher 4 is EATING
Philosopher 1 is THINKING
Philosopher 2 is EATING
Philosopher 0 is HUNGRY
Philosopher 0 is WAITING
Philosopher 4 is THINKING
Philosopher 0 is EATING
Philosopher 3 is HUNGRY
Philosopher 3 is WAITING
Philosopher 2 is THINKING
Philosopher 3 is EATING
Philosopher 1 is HUNGRY
Philosopher 1 is WAITING
Philosopher 4 is HUNGRY
Philosopher 4 is WAITING
Philosopher 0 is THINKING
Philosopher 1 is EATING
Philosopher 2 is HUNGRY
Philosopher 2 is WAITING
Philosopher 3 is THINKING
Philosopher 4 is EATING
Philosopher 0 is HUNGRY
Philosopher 0 is WAITING
Philosopher 1 is THINKING
Philosopher 2 is EATING
Philosopher 4 is THINKING
Philosopher 0 is EATING
Philosopher 2 is THINKING
Philosopher 1 is HUNGRY
Philosopher 1 is WAITING
Philosopher 0 is THINKING
Philosopher 1 is EATING
Philosopher 1 is THINKING
----------------------------------------------------------------------------------------------------
Ended

但是,我已经检查过你在这样的一些特殊情况下出现了僵局:当所有哲学家至少有一个可以吃饭而其他人都在等待时。但是我已经通过在函数的标头中使用同步来更改测试中的代码,通过 while 和在方法 putdown() 中使用 test() 方法的 if 条件,我已经通过 notifyAll() 更改了通知;代码是这样的:

class MyMonitor {
private enum States {THINKING, HUNGRY, EATING};
private  States[] state;

public MyMonitor() {
    state = new States[5];
    for(int i = 0; i < 5; i++) {
        state[i] = States.THINKING;
        System.out.println("Philosopher " + i + " is THINKING");
    }
}

private synchronized void test(int i) {
    while((state[(i+4)%5]!=States.EATING) && (state[i]==States.HUNGRY) && (state[(i+1)%5]!=States.EATING)) {
        state[i] = States.EATING;
        System.out.println("Philosopher " + i + " is EATING");
     //   notify();
    }
}

public synchronized void pickup(int i) {
        state[i] = States.HUNGRY;
        System.out.println("Philosopher " + i + " is HUNGRY");      
        test(i);
        if (state[i] != States.EATING) {
            System.out.println("Philosopher " + i + " is WAITING");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
}

public synchronized void putdown(int i) {
        state[i] = States.THINKING;
        System.out.println("Philosopher " + i + " is THINKING");
        //test((i+4)%5);
        //test((i+1)%5);
        notifyAll();
    }
}

我建议您使用一个或多个实现,然后再考虑要打破什么科夫曼条件。祝你好运

于 2018-03-26T21:21:34.130 回答