Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9032475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:59:24+00:00 2026-06-16T07:59:24+00:00

My java project has 3 java class. There is Main,Philosoper and chStick. I solved

  • 0

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();
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:59:25+00:00Added an answer on June 16, 2026 at 7:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So for my current project, there are basically three main Java classes: GUI Instant
my problem is about my GWT project. i got 2 files named Main.java(EntryPoint) and
I've a Java project which has some resources located in a folder named files
i have a java ee project which has a text file that uses a
My Project has 2 java files (A.java and B.java in same package). A.java uses
I have a Java project with a toolbar, and the toolbar has icons on
Let's say I make a Java project in Eclipse that has 3-10 classes and
I am trying to compile a Maven Java/Scala mixed project that has a Scala
I am working on a java ant + ivy based project that has the
I have a java application build upon Spring 3. This project has another jar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.