dining philosophers problem in java

Dining Philosophers Problem: Consider five philosophers who spend time in thinking and eating.the philosophers share a common circular table surrounded by five chairs,each belonging to one philosopher.In the center of the table is a bowl of rice and the table is laid with five single chopsticks.when a philosopher thinks she does not interacts with her colleagues.From time to time philosopher gets hungry and tries to pick up the two chopsticks that are between her left and right philosopher. philosopher may pick up only one chopstick at a time.Obviously,she cannot pick chopstick in hand of neighbor.When hungry philosopher has both her chopstick at the same time she eats without releasing her chopstick.When she is finished eating she puts down chopstick and start thinking.

import java.io.*;
class Philo
int state[]=new int[5];
int thinking,eating,hungry;
void Op()
public void pickup(int i)
System.out.println(“Philosopher “+i+” is hungry”);
System.out.println(“Philosopher “+i+” is waiting”);
public void putdown(int i)
System.out.println(“Philosopher”+i+” is thinking”);

public void test(int i)
if(state[i]==2 && state[(i+1)%5]!=1 && state[(i+4)%5]!=1)
System.out.println(“philosopher “+i+” is eating”);
class Philosopher
public static void main(String args[])throws IOException
Philo d=new Philo();
for(int i=0;i<5;i++)
for(int i=0;i<5;i++)

/* Output for dining philosophers problem

Philosopher 0 is hungry
philosopher 0 is eating
Philosopher 1 is hungry
Philosopher 1 is waiting
Philosopher 2 is hungry
philosopher 2 is eating
Philosopher 3 is hungry
Philosopher 3 is waiting
Philosopher 4 is hungry
Philosopher 4 is waiting
Philosopher0 is thinking
Philosopher2 is thinking
philosopher 3 is eating
Philosopher3 is thinking
philosopher 4 is eating
Philosopher4 is thinking

Facing difficulties in understanding the program ?

Ask Your Queries in Comment Box


Founder of Java Engineering Programs. Follow me on Google+ ,Linked in.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *