My code is

package diningphilosophers;

class DiningPhilosophers {           // Η κλάση αναπαριστά την εφαρμογή
    static Philosopher philosopher[];
    static Forks forks;
    static DiningRoom diningRoom;
    
    public static void main(String[] argv) {
        philosopher = new Philosopher[5];
        forks = new Forks();
        diningRoom = new DiningRoom();

        for(int i = 0; i < 5; i++)
            philosopher[i] = new Philosopher(i, 10, 20, 30, 100);
        for(int i = 0; i < 5; i++)
            philosopher[i].start();
    }
}

class Philosopher extends Thread {   // Η κλάση αναπαριστά το φιλόσοφο
    int i;
    int minThinkTime, maxThinkTime, minEatTime, maxEatTime;

    public Philosopher(int index, int minThinkTime, int maxThinkTime, int minEatTime, int maxEatTime) {
        this.i = index;
        this.minThinkTime = minThinkTime;
        this.maxThinkTime = maxThinkTime;
        this.minEatTime = minEatTime;
        this.maxEatTime = maxEatTime;
    }

    public void think() {
        try {
            Thread.sleep((int)(Math.random() * (maxThinkTime - minThinkTime)) + minThinkTime);
        }
        catch (InterruptedException e) { }
    }
    public void eat() {
        try {
            Thread.sleep((int)(Math.random() * (maxEatTime - minEatTime)) + minEatTime);
        }
        catch(InterruptedException e) { }
    }

    @Override
    public void run() {
        while(true) {
            think();
            diningRoom.enter();
            forks.take(i);
            eat();
            forks.release(i);
            diningRoom.exit();
        }
    }
}

class Forks {                        // Η κλάση αναπαριστά έναν ελεγκτή ο  οποίος χρησιμοποιείται για να διασφαλίσει
                                            // τον αμοιβαίο αποκλεισμό στη χρήση των κοινών πόρων που είναι τα πιρούνια
    private int numOfForks[];
    
    public Forks() {
        numOfForks = new int[5];
        for(int i = 0; i < 5; i++)
            numOfForks[i] = 2;
    }

    public synchronized void take(int philosopher) {
        while(numOfForks[philosopher] != 2) {
            try {
                wait();
            }
            catch (InterruptedException e) { }
        }
        numOfForks[(philosopher + 1)%5]--;
        numOfForks[(philosopher - 1)%5]--;
    }
    public synchronized void release(int philosopher) {
        numOfForks[(philosopher + 1)%5]++;
        numOfForks[(philosopher - 1)%5]++;
        notifyAll();
    }
}

class DiningRoom {                   // Η κλάση αναπαριστά μια εικονική τραπεζαρία
    private int vacancy;

    public DiningRoom() {
        vacancy = 5 - 1;
    }
    public synchronized void enter() {
        while (vacancy == 0) {
            try {
                wait();
            }
            catch(InterruptedExeption e) { }
        }
        vacancy--;
    }
    public synchronized void exit() {
        vacancy++;
        notify();
    }
}

This is the dining philosophers problem. Where is my fault? I think i need an import but i don't know which.

Where is object reference variable:

class Philosopher extends Thread {  
    int i;
    int minThinkTime, maxThinkTime, minEatTime, maxEatTime;
    ....
    ....
    @Override
    public void run() {
        while(true) {
            think();
            // diningRoom and forks are not declared !!!
            diningRoom.enter();    
            forks.take(i);
            eat();
            forks.release(i);
            diningRoom.exit();
        }
    }
}

adatapost firstly thanks for your help.
Secondly how can i do that?

adatapost firstly thanks for your help.
Secondly how can i do that?

Sure, they are declared in DiningPhilosophers class. If you want to use them in your another class then you must have qualify with its class name.

class Philosopher extends Thread {  
    int i;
    int minThinkTime, maxThinkTime, minEatTime, maxEatTime;
    ....
    ....
    @Override
    public void run() {
        while(true) {
            think();
            // diningRoom and forks are not declared !!!
            DiningPhilosophers.diningRoom.enter();    
            DiningPhilosophers.forks.take(i);
            eat();
            DiningPhilosophers.forks.release(i);
            DiningPhilosophers.diningRoom.exit();
        }
    }
}

adatapost thanks. I didn't know that...it's interesting.

Now there is a problem with method enter()
The error in output is the following:

init:
deps-clean:
Deleting directory /home/dimproun/NetBeansProjects/DiningPhilosophers/build
clean:
init:
deps-jar:
Created dir: /home/dimproun/NetBeansProjects/DiningPhilosophers/build/classes
Compiling 1 source file to /home/dimproun/NetBeansProjects/DiningPhilosophers/build/classes
/home/dimproun/NetBeansProjects/DiningPhilosophers/src/diningphilosophers/Main.java:96: cannot find symbol
symbol  : class InterruptedExeption
location: class diningphilosophers.DiningRoom
            catch(InterruptedExeption e) { }
1 error
BUILD FAILED (total time: 1 second)

Now what can i do?

Edited 3 Years Ago by Reverend Jim: Fixed formatting

You can import the class where "InterruptedExeption" is located. You're getting the error "cannot find symbol, class InterruptedExeption" because the compiler does not know what that class is. It doesn't know because you have not imported the resources. And "InterruptedExeption" is obviously a class you created, because you spelled Exception incorrectly. . so find what package it is in and put import packageName.*; at the top of the file that's giving you the error.

Dear,

java.lang package classes are imported automatically. You misspell InterruptedException :

public synchronized void enter() {
        while (vacancy == 0) {
            try {
                wait();
            }
            catch(InterruptedExeption e) { }
        }

You are right. Huge mistake.
I push the button "run" and and to output it is appeared to me the following message:

run:
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: -1
at diningphilosophers.Forks.take(Main.java:78)
at diningphilosophers.Philosopher.run(Main.java:52)

After six minutes it is still running. Is it ok?

I've never heard of a program being "ok" when an Exception is thrown and not caught. That error that you got means you tried to access an array at the index "-1", but "-1" isn't a valid array index, so that can never work. If you posted your code in Java code tags like you're supposed to I'd track it down for you, but since you didn't... meh.

The code now is

package diningphilosophers;

//import java.lang.*;

class DiningPhilosophers {         
    static Philosopher philosopher[];
    static Forks forks;
    static DiningRoom diningRoom;
    
    public static void main(String[] argv) {
        philosopher = new Philosopher[5];
        forks = new Forks();
        diningRoom = new DiningRoom();

        for(int i = 0; i < 5; i++)
            philosopher[i] = new Philosopher(i, 10, 20, 30, 100);
        for(int i = 0; i < 5; i++)
            philosopher[i].start();
    }
}

class Philosopher extends Thread {  
    int i;
    int minThinkTime, maxThinkTime, minEatTime, maxEatTime;

    public Philosopher(int index, int minThinkTime, int maxThinkTime, int minEatTime, int maxEatTime) {
        this.i = index;
        this.minThinkTime = minThinkTime;
        this.maxThinkTime = maxThinkTime;
        this.minEatTime = minEatTime;
        this.maxEatTime = maxEatTime;
    }

    public void think() {
        try {
            Thread.sleep((int)(Math.random() * (maxThinkTime - minThinkTime)) + minThinkTime);
        }
        catch (InterruptedException e) { }
    }
    public void eat() {
        try {
            Thread.sleep((int)(Math.random() * (maxEatTime - minEatTime)) + minEatTime);
        }
        catch(InterruptedException e) { }
    }

    @Override
    public void run() {
        while(true) {
            think();
             DiningPhilosophers.diningRoom.enter();
             DiningPhilosophers.forks.take(i);
             eat();
             DiningPhilosophers.forks.release(i);
             DiningPhilosophers.diningRoom.exit();
        }
    }
}

class Forks {                      
    private int numOfForks[];
    
    public Forks() {
        numOfForks = new int[5];
        for(int i = 0; i < 5; i++)
            numOfForks[i] = 2;
    }

    public synchronized void take(int philosopher) {
        while(numOfForks[philosopher] != 2) {
            try {
                wait();
            }
            catch (InterruptedException e) { }
        }
        numOfForks[(philosopher + 1)%5]--;
        numOfForks[(philosopher - 1)%5]--;
    }
    public synchronized void release(int philosopher) {
        numOfForks[(philosopher + 1)%5]++;
        numOfForks[(philosopher - 1)%5]++;
        notifyAll();
    }
}

class DiningRoom { 
    private int vacancy;

    public DiningRoom() {
        vacancy = 5 - 1;
    }
    public synchronized void enter() {
        while (vacancy == 0) {
            try {
                wait();
            }
            catch(InterruptedException e) { }
        }
        vacancy--;
    }
    public synchronized void exit() {
        vacancy++;
        notify();
    }
}

I've never heard of a program being "ok" when an Exception is thrown and not caught. That error that you got means you tried to access an array at the index "-1", but "-1" isn't a valid array index, so that can never work. If you posted your code in Java code tags like you're supposed to I'd track it down for you, but since you didn't... meh.

** A little while later, when you still did not post your code in Java code tags **

no one?

...

You should read the thread stickied at the top of this forum that explains how to post in Java code tags. Its basically for the first part, then leave the second one the same.[CODE=Java] for the first part, then leave the second one the same.

package diningphilosophers;

//import java.lang.*;

class DiningPhilosophers {           // Η κλάση αναπαριστά την εφαρμογή
    static Philosopher philosopher[];
    static Forks forks;
    static DiningRoom diningRoom;

    public static void main(String[] argv) {
        philosopher = new Philosopher[5];
        forks = new Forks();
        diningRoom = new DiningRoom();

        for(int i = 0; i < 5; i++)
            philosopher[i] = new Philosopher(i, 10, 20, 30, 100);
        for(int i = 0; i < 5; i++)
            philosopher[i].start();
    }
}

class Philosopher extends Thread {   // Η κλάση αναπαριστά το φιλόσοφο
    int i;
    int minThinkTime, maxThinkTime, minEatTime, maxEatTime;

    public Philosopher(int index, int minThinkTime, int maxThinkTime, int minEatTime, int maxEatTime) {
        this.i = index;
        this.minThinkTime = minThinkTime;
        this.maxThinkTime = maxThinkTime;
        this.minEatTime = minEatTime;
        this.maxEatTime = maxEatTime;
    }

    public void think() {
        try {
            Thread.sleep((int)(Math.random() * (maxThinkTime - minThinkTime)) + minThinkTime);
        }
        catch (InterruptedException e) { }
    }
    public void eat() {
        try {
            Thread.sleep((int)(Math.random() * (maxEatTime - minEatTime)) + minEatTime);
        }
        catch(InterruptedException e) { }
    }

    @Override
    public void run() {
        while(true) {
            think();
             DiningPhilosophers.diningRoom.enter();
             DiningPhilosophers.forks.take(i);
             eat();
             DiningPhilosophers.forks.release(i);
             DiningPhilosophers.diningRoom.exit();
        }
    }
}

class Forks {                        // Η κλάση αναπαριστά έναν ελεγκτή ο  οποίος χρησιμοποιείται για να διασφαλίσει
                                            // τον αμοιβαίο αποκλεισμό στη χρήση των κοινών πόρων που είναι τα πιρούνια
    private int numOfForks[];

    public Forks() {
        numOfForks = new int[5];
        for(int i = 0; i < 5; i++)
            numOfForks[i] = 2;
    }

    public synchronized void take(int philosopher) {
        while(numOfForks[philosopher] != 2) {
            try {
                wait();
            }
            catch (InterruptedException e) { }
        }
        numOfForks[(philosopher + 1)%5]--;
        numOfForks[(philosopher - 1)%5]--;
    }
    public synchronized void release(int philosopher) {
        numOfForks[(philosopher + 1)%5]++;
        numOfForks[(philosopher - 1)%5]++;
        notifyAll();
    }
}

class DiningRoom {                   // Η κλάση αναπαριστά μια εικονική τραπεζαρία
    private int vacancy;

    public DiningRoom() {
        vacancy = 5 - 1;
    }
    public synchronized void enter() {
        while (vacancy == 0) {
            try {
                wait();
            }
            catch(InterruptedException e) { }
        }
        vacancy--;
    }
    public synchronized void exit() {
        vacancy++;
        notify();
    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

OK you should have posted it using

[code=java] [/code]

package diningphilosophers; 
class DiningPhilosophers
{
  static Philosopher philosopher[];
  static Forks forks;
  static DiningRoom diningRoom;

  public static void main ( String[] argv )
  {
    philosopher = new Philosopher[5];
    forks = new Forks();
    diningRoom = new DiningRoom();

    for ( int i = 0; i < 5; i++ )
      philosopher[i] = new Philosopher ( i, 10, 20, 30, 100 );
    for ( int i = 0; i < 5; i++ )
      philosopher[i].start();
  }
}

class Philosopher extends Thread
{
  int i;
  int minThinkTime, maxThinkTime, minEatTime, maxEatTime;

  public Philosopher ( int index, int minThinkTime, int maxThinkTime, int minEatTime, int maxEatTime )
  {
    this.i = index;
    this.minThinkTime = minThinkTime;
    this.maxThinkTime = maxThinkTime;
    this.minEatTime = minEatTime;
    this.maxEatTime = maxEatTime;
  }

  public void think()
  {
    try
    {
      Thread.sleep ( ( int ) ( Math.random() * ( maxThinkTime - minThinkTime ) ) + minThinkTime );
    }
    catch ( InterruptedException e )
    { }
  }
  public void eat()
  {
    try
    {
      Thread.sleep ( ( int ) ( Math.random() * ( maxEatTime - minEatTime ) ) + minEatTime );
    }
    catch ( InterruptedException e )
    { }
  }

  //@Override
  public void run()
  {
    while ( true )
    {
      think();
      DiningPhilosophers.diningRoom.enter();
      DiningPhilosophers.forks.take ( i );
      eat();
      DiningPhilosophers.forks.release ( i );
      DiningPhilosophers.diningRoom.exit();
    }
  }
}

class Forks
{
  private int numOfForks[];

  public Forks()
  {
    numOfForks = new int[5];
    for ( int i = 0; i < 5; i++ )
      numOfForks[i] = 2;
  }

  public synchronized void take ( int philosopher )
  {
    while ( numOfForks[philosopher] != 2 )
    {
      try
      {
        wait();
      }
      catch ( InterruptedException e )
      { }
    }
    numOfForks[ ( philosopher + 1 ) %5]--;
    numOfForks[ ( philosopher - 1 ) %5]--;
  }
  public synchronized void release ( int philosopher )
  {
    numOfForks[ ( philosopher + 1 ) %5]++;
    numOfForks[ ( philosopher - 1 ) %5]++;
    notifyAll();
  }
}

class DiningRoom
{
  private int vacancy;

  public DiningRoom()
  {
    vacancy = 5 - 1;
  }
  public synchronized void enter()
  {
    while ( vacancy == 0 )
    {
      try
      {
        wait();
      }
      catch ( InterruptedException e )
      { }
    }
    vacancy--;
  }
  public synchronized void exit()
  {
    vacancy++;
    notify();
  }
}

I'll do it for you this time.

Aha I understand. Now please help me to put the mistake right.

This article has been dead for over six months. Start a new discussion instead.