Question

My java project has 3 java class. There is Main,Philosoper and chStick. I solved Dining Philosophers Problem with using boolean value isTaken.

in the project chStick.java like below. This class control that chopstick in use or not use.

class ChStick
{
   private int id;
   private boolean isTaken;

   ChStick (int id)
   {  
       this.id=id;
   }

   synchronized void get () throws InterruptedException
   {
       while(isTaken){
          wait();
       }

       isTaken=true;
   }

   int getID ()
   {
       return id;
   }

   synchronized void put () throws InterruptedException
   {
      isTaken=false;
   }
}

This code works succesfully.When I use Semaphore instead of boolean value isTaken, it is also works succesfully.

My problem is rewrite this class using Mutex. I tried lots of methods but it has still not worked. It must use Mutex class .

Edit:

class Philos extends Thread// #9 - Make this class to be inherited from Thread class
{
   private String name;
   private ChStick left, right;

   Philos (String name, ChStick left, ChStick right)
   {
     // #10 - Assign this philosopher a name, his left and rigt chopsticks.
       this.left=left;
       this.right=right;
       this.name=name;
   }

    @Override
   public void run ()
   {
       while (true)
       {
          try
          {
              System.out.println ("Philosopher " + name + " is thinking.");

              // #11 - Make philosopher sleep for a RANDOM time.
              Thread.sleep(1000);

              System.out.println ("Philosopher " + name + " is hungry.");



              if (left.getID () < right.getID ())
              {
                  System.out.println ("Philosopher " + name +
                                      " getting left ChStick.");

            // #12 - Make philosopher to get left chopstick.
                  left.get();

                  System.out.println ("Philosopher " + name +
                                      " got left ChStick."+left.getID());
              }
              else
              {
                  System.out.println ("Philosopher " + name +
                                      " getting right ChStick.");

                  // #13 - Make philosopher to get right chopstick.
                  right.get();

                  System.out.println ("Philosopher " + name +
                                      " got right ChStick."+right.getID());
              }

              // Get other ChStick.

              if (left.getID () < right.getID ())
              {
                  System.out.println ("Philosopher " + name +
                                      " getting right ChStick.");

            // #14 - Make philosopher to get (left or right)? chopstick.
                  right.get();

                  System.out.println ("Philosopher " + name +
                                      " got right ChStick."+right.getID());
              }
              else
              {
                  System.out.println ("Philosopher " + name +
                                      " getting left ChStick.");

                  // #15 - Make philosopher to get (left or right)? chopstick.
                  left.get();


                  System.out.println ("Philosopher " + name +
                                      " got left ChStick."+left.getID());
              }

              System.out.println ("Philosopher " + name + " is eating.");

              // #16 - Make philosopher sleep for a RANDOM time.
              Thread.sleep(2000);

              // #17 - Make philosopher to put the left chopstick down.

              System.out.println ("Philosopher " + name +
                                  " putting down left ChStick."+left.getID());
              left.put();

              // #17 - Make philosopher to put the right chopstick down.

              System.out.println ("Philosopher " + name +
                                  " putting down right ChStick."+right.getID());
              right.put();
          } 
          catch (InterruptedException e) {}
       }
   }
}



public class Main
{
   public static void main (String [] args)
   {                                                 

    // #1 - Here you should create chopstick instances 
    //      and number them using the structure defined in its class definition. 

    // ChStick cs1 = new ChStick ..
       ChStick cs1 = new ChStick(1);
       ChStick cs2 = new ChStick(2);
       ChStick cs3 = new ChStick(3);
       ChStick cs4 = new ChStick(4);
       ChStick cs5 = new ChStick(5);


    // #2 - Here you should create Philosopher instances
    //      and assign them names, their right and left chopsticks.
    //      BE CAREFUL IN WHICH ORDER THEY SIT AROUND THE TABLE 

    // Philos p1 = new Philos ("P1"...)
      Philos p1 = new Philos("P1",cs1,cs5);
      Philos p2 = new Philos("P2",cs2,cs1);
      Philos p3 = new Philos("P3",cs3,cs2);
      Philos p4 = new Philos("P4",cs4,cs3); 
      Philos p5 = new Philos("P5",cs5,cs4);

    // #3 - Start p
      p1.start();
      p2.start();
      p3.start();
      p4.start();
      p5.start();
   }
}
Was it helpful?

Solution

I guess what you are looking for is a variation on the Conductor solution.

Your mutex would function as the conductor, but would allow only one philosopher at a time to take forks and dine.

So any phislopher thread would wait for a successful acquire() on the mutex, take the forks which must be available since no other philosopher is dining. Then dine, put the forks down, and call release() on the mutex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top