0

Main.java:

package esempio2;

public class Main {

    public static void main(String args[]){
        int numFilosofi = 5;
        Forchetta forchette[] = new Forchetta[numFilosofi];
        Filosofo filosofi[] = new Filosofo[numFilosofi];

        for(int i=0;i<numFilosofi;++i){
            forchette[i] = new Forchetta(i);
        }

        int sxForkIndex,dxForkIndex;
        for(int i=0;i<numFilosofi;++i){
            sxForkIndex = i-1;
            dxForkIndex = i;
            if (i==0)
                sxForkIndex = numFilosofi-1;
            filosofi[i] = new Filosofo(forchette[sxForkIndex],forchette[dxForkIndex],i);
            filosofi[i].start();
            
        }
    }

}

Filosofo.java (it should break the circular wait because I am making the thread 0 doing something different!):

package esempio2;

public class Filosofo extends Thread{
    int id;
    Forchetta forchettaSx;
    Forchetta forchettaDx;

    public Filosofo(Forchetta forchettaSx, Forchetta forchettaDx, int id){
        this.id = id;
        this.forchettaSx = forchettaSx;
        this.forchettaDx = forchettaDx;
    }

    @Override
    public void run(){

        while(true){
            if (this.id==0){ // THIS SHOULD BREAK THE CIRCULAR WAIT
                this.forchettaSx.prendiFork(this.id);
                System.out.println("Il filosofo num "+this.id+" ha preso forchetta sx num "+this.forchettaSx.id);
                this.forchettaDx.prendiFork(this.id);
                System.out.println("Il filosofo num "+this.id+" ha preso forchetta dx num "+this.forchettaDx.id);
                this.forchettaSx.posaFork(this.id);
                System.out.println("Il filosofo num "+this.id+" ha posato forchetta sx num "+this.forchettaSx.id);
                this.forchettaDx.posaFork(this.id);
                System.out.println("Il filosofo num "+this.id+" ha posato forchetta dx num "+this.forchettaDx.id);
            } else{
                this.forchettaDx.prendiFork(this.id);   
                System.out.println("Il filosofo num "+this.id+" ha preso forchetta dx num "+this.forchettaDx.id);
                this.forchettaSx.prendiFork(this.id);
                System.out.println("Il filosofo num "+this.id+" ha preso forchetta sx num "+this.forchettaSx.id);
                this.forchettaDx.posaFork(this.id);
                System.out.println("Il filosofo num "+this.id+" ha posato forchetta Dx num "+this.forchettaDx.id);
                this.forchettaSx.posaFork(this.id);
                System.out.println("Il filosofo num "+this.id+" ha posato forchetta Sx num "+this.forchettaSx.id);
            }
        }
    }
}

Forchetta.java

package esempio2;

public class Forchetta {
    int id;
    boolean libera = true;

    public Forchetta(int id){
        this.id=id;
    }

    public synchronized void prendiFork(int idFilosofo){
        while(libera==false){
            try{
                System.out.println("Il filosofo num "+idFilosofo+" NON puo prendere forchetta num "+this.id);
                wait();
            } catch(InterruptedException e){
                e.printStackTrace();
            }
        }
        this.libera=false;

    }

    public synchronized void posaFork(int idFilosofo){
        this.libera=true;
        notifyAll();
    }
}

Basically, if I remove the if statement and make all the threads do the same thing, it breaks immediately and goes deadlock. However, If I add that if, it doesn't freeze immediately, but only after some minutes. Why does this happen?

If I write if (this.id%2 == 0) instead of if (this.id==0), it seems not to freeze! Why?

Allexj
  • 1,375
  • 6
  • 14
  • 29
  • Are you the same person who asked this question yesterday, and got a very thorough, detailed answer? – Dawood ibn Kareem May 30 '22 at 08:39
  • @DawoodibnKareem I didn't get any answer – Allexj May 30 '22 at 08:44
  • Your question was answered, but you deleted your question afterwards... – Lajos Arpad May 30 '22 at 08:50
  • You got a very long answer from someone called Dan. Now, the original question and Dan's answer have both been deleted. I don't know why. Asking the same question multiple times isn't really a good way to encourage people to answer. Have you considered offering a bounty instead? – Dawood ibn Kareem May 30 '22 at 08:50
  • @DawoodibnKareem it was deleted by the post author, so Allexj have seen the answer, removed the question and now claims that he didn't get any answers. – Lajos Arpad May 30 '22 at 08:51
  • 1
    Actually @LajosArpad, that means that Dan deleted it themselves. But it was certainly up there for at least 23 hours. – Dawood ibn Kareem May 30 '22 at 08:52
  • Allexj, you need to follow the advice that tgdavies gave you in the original copy of your question. Look at the logs to tell which philosopher is locking which. – Dawood ibn Kareem May 30 '22 at 09:00
  • @DawoodibnKareem I know, I was careful not to accuse him though and to present this factually. – Lajos Arpad May 30 '22 at 09:09
  • @DawoodibnKareem yeah I deleted it, because my answer was wrong and I didn't feel to keep it there. After further investigations, thanks to DawoodibnKareem, I've realized that it wasn't the missing philosopher's id within the Forchetta class the actual problem. Unfortunately, instead of copying his code I've written my own version and then compared them (big mistake!). This morning I've also copied your code Alex from the previous answer and I didn't have a single deadlock... I don't know what's wrong with it. – Dan May 30 '22 at 09:15
  • @LajosArpad I didn't received any answer. So I removed the post. Actually it was showing like if there was answer, but when I refreshed the page the answer was gone, so I guess the guy who answered deleted it – Allexj May 30 '22 at 09:18
  • @Dan did you copy the exact code? After how much time did you wait? I noticed the deadlock after some minutes in my testing – Allexj May 30 '22 at 09:21
  • 1
    I second, just copied and run: no deadlock after more than 15 minutes (kind of forgot it running while on a meeting) ((maybe different number of used CPU cores? or additional activity on the computer? or IDE, console output?)) – user16320675 May 30 '22 at 09:22
  • https://meta.stackoverflow.com/q/261078 – Dawood ibn Kareem May 30 '22 at 09:22
  • 1
    @Allexj a couple of minutes, to be honest. How long did you leave it running? I don't think it matters anymore, user16320675 already answered for us. I don't think this problem is reproducible. – Dan May 30 '22 at 09:22
  • Also - https://meta.stackoverflow.com/q/329476 https://meta.stackoverflow.com/q/288854 – Dawood ibn Kareem May 30 '22 at 09:29
  • @Allexj this is why I have avoided your accusation, I have given you the chance to explain it and I find your explanation convincing, so, you had an answer, reloaded it, it disappeared and now you said you didn't receive an answer, because it appeared to you only briefly. But then you received an answer after all, it just disappeared. Is this what happened? – Lajos Arpad May 30 '22 at 09:30
  • 1
    started after my last comment, on IDE and on command line, both are still running, no deadlock – user16320675 May 30 '22 at 09:56
  • @user16320675 thanks, that's just strange. however, my computer has 8 cores – Allexj May 30 '22 at 10:15
  • Does this answer your question? [What is the difference between atomic / volatile / synchronized?](https://stackoverflow.com/questions/9749746/what-is-the-difference-between-atomic-volatile-synchronized) – pringi Jun 01 '22 at 12:22

0 Answers0