这是类(Class)哲学家

public class Filosofo implements Runnable{

    public String nome = null;
    public static Bacchetta[] bacchette = new Bacchetta[5]; //this should be the resource
    public Bacchetta bacchettaDX; //right resource
    public Bacchetta bacchettaSX; //left resource
    public static int indice = 0;

    public int x[] = {};
    public int y[] = {};

    public JButton filosofo = new JButton();  //Button associated to the philos.

    public Filosofo(String nome, int SX, int DX){
        indice++;
        for(int i = 0; i<5; i++){
            bacchette[i] = new Bacchetta();
        }
        this.nome = nome;
        this.bacchettaSX = bacchette[SX];
        this.bacchettaDX = bacchette[DX];
    }

    @Override
    public synchronized void  run() {
        Random r = new Random();
        int random;

        while(true){
            random = (int) r.nextInt(100);
            pensa(5000);
            random = (int) r.nextInt(100);
            mangia(5000);

        }
    }


    //the method mangia means the phil. is eating, so has both chopsticks
    public synchronized void mangia(int tempo){

        do{
        if(!bacchettaSX.isOccupied){
            bacchettaSX.isOccupied = true;
            bacchettaSX.setChiOccupa(this.nome);
        }
        if(!bacchettaDX.isOccupied){
            bacchettaDX.isOccupied = true;
            bacchettaDX.setChiOccupa(this.nome);
        }
        }while(bacchettaSX.getChiOccupa().compareTo(this.nome) != 0 && bacchettaDX.getChiOccupa().compareTo(this.nome) != 0);

        this.filosofo.setBackground(Color.GREEN);
        try {
            sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Filosofo.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("\t\t\t" + this.nome + " sta mangiando");
        int a = 0;
        /*for(long i = 0; i<1000000000; i++){
            a++;
        }*/



        bacchettaSX.isOccupied = false;
        bacchettaDX.isOccupied = false;
        bacchettaSX.setChiOccupa(null);
        bacchettaDX.setChiOccupa(null);

        System.out.println("\t\t\t\t\t\t" + this.nome + " ha finito di mangiare");
        this.filosofo.setBackground(Color.BLUE);

    }
    //the method pensa means the philosopher is no longer eating
    public void pensa(int tempo){
        System.out.println(this.nome + " sta ponderando");
        try {
            sleep(tempo);
        } catch (InterruptedException ex) {
            Logger.getLogger(Filosofo.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

应该在终端上打印出正在做什么,问题是他们应该一个一吃,或者在最佳情况下最多吃两个哲学家。但是,他们一起吃。同步未完成应做的工作。问题出在哪儿?

最佳答案

您的代码块

for(int i = 0; i<5; i++){
    bacchette[i] = new Bacchetta();
}

每次创建新的哲学家时都要初始化静态数组,因此它们都以不同的bachettaSX和bachettaDX结尾。
在构造函数外部的静态代码块中对其进行一次初始化。
static {
  for(int i = 0; i<5; i++){
    bacchette[i] = new Bacchetta();
  }
}

关于java - 我的五位哲学家都在同一时间吃饭,为什么呢?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30175870/

10-12 22:28