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 6967481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:19:38+00:00 2026-05-27T16:19:38+00:00

I have implement dining philosophers problem in java but something goes wrong and it

  • 0

I have implement dining philosophers problem in java but something goes wrong and it leads to deadlock…
Can someone spot why this leads to deadlock?

The philosophers should always be N-1 in the dining room so everyone can eat and prevent starvation.

The main class:

public class DiningPhilosophers {

public static int N = 5;
//Philosopher philosopher[];
//Chopsticks chopsticks;
//DiningRoom diningroom;


public static void main(String[] args) {
    Philosopher[] philosopher = new Philosopher[N];
    Chopsticks chopsticks = new Chopsticks(N);
    DiningRoom diningroom = new DiningRoom(N);

    for(int i = 0;i<N;i++)
        philosopher[i] = new Philosopher(i,10,20,30,100,chopsticks,diningroom);

    for(int i = 0;i<N;i++)
        philosopher[i].start();


}

}

The Chopstick class:

class Chopsticks{


private int numOfChops[];
private int N;

public Chopsticks(int N){
    this.N = N;
    numOfChops = new int[N];
    for(int i = 0;i<N;i++)
        numOfChops[i] = 2;

}

public synchronized void take(int philosopher){
    while(numOfChops[philosopher] != 2){
        try{
            wait();
        }catch(InterruptedException e){

        }
        //System.out.println("philosopher "+philosopher+"taking");
        numOfChops[(philosopher+1)%N]--;
        numOfChops[(Math.abs(philosopher-1))%N]--;
    }
}

public synchronized void release(int philosopher){
    //System.out.println("philosopher "+philosopher+"releasing");
    numOfChops[(philosopher+1)%N]++;
    numOfChops[(Math.abs(philosopher-1))%N]++;
    notifyAll();
}
}

The dining room class:

class DiningRoom{
private int vacancy;
private int n;

public DiningRoom(int N){
    this.n = N;
    vacancy = n -1;

}

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

public synchronized void exit(){

    vacancy++;
    notify();

}
}

The philosopher class:

class Philosopher extends Thread{

int i;
int minThinkTime,maxThinkTime,minEatTime,maxEatTime;
private Chopsticks c;
private DiningRoom d;

public Philosopher(int index,int minThinkTime,int maxThinkTime,int minEatTime,int maxEatTime, Chopsticks chopsticks, DiningRoom diningroom){
    this.i = index;
    this.minThinkTime = minThinkTime;
    this.maxThinkTime = maxThinkTime;
    this.minEatTime = minEatTime;
    this.maxEatTime = maxEatTime;
    this.c = chopsticks;
    this.d = diningroom;
} 

public void think(){
    try{
        System.out.println(i+" Philosopher is thinking!");
        Thread.sleep((int)(Math.random()*(maxThinkTime - minThinkTime))+minThinkTime);

    }catch(InterruptedException e){

    }
}

public void eat(){
    try{
        System.out.println(i+" Philosopher is eating!");
        Thread.sleep((int)(Math.random()*(maxEatTime - minEatTime))+minEatTime);

    }catch(InterruptedException e){

    }
}
public void run() {
    while (true) {
         think();
         System.out.println("pholosopher "+i+"entering");
         d.enter();
         c.take(i);
         eat();
         c.release(i);
         d.exit();
         System.out.println("pholosopher "+i+"exiting");
    }
}


}
  • 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-05-27T16:19:39+00:00Added an answer on May 27, 2026 at 4:19 pm

    in:

    public synchronized void take(int philosopher)

    you should move the lines that decrease the chopstick counters outside the loop.

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

Sidebar

Related Questions

I have to implement the VinPower application. They offer a Java version, a C
I have to implement a one linked list but it should put object in
I have implement paypal library for payment in my iphone project. How can I
I have implement the simple TCP server and TCP client classes which can send
I have implement something like the following on J2me. Is it possible to implement
How can i sort ArrayList in java without using comparator.? I have already used
I have implement the ValueChanged event, but I found it will be triggered only
I have implement a scrollview with paging but it not work ok. For every
I have to implement gtalk like chat application using UDP in java. i have
I have to implement some code in Ruby based on Java implementation that was

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.