Class App
import javax.swing.*;
public class App {
JFrame f1;
Chopstick chopstick[];
Philosopher philosophers[];
public App(){
f1=new JFrame();
f1.setTitle("DINING PHILOSOPHER SIMULATOR | DEVELOPED BY MD.SHAHARYAR SHAUKAT");
f1.setSize(1100, 640);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setLocationRelativeTo(null);
f1.setResizable(false);
initializePos();
initializePhilosopher();
Display obj=new Display();
obj.setBounds(0, 0, 1040, 600);
Thread t1=new Thread(obj);
t1.start();
new Thread(philosophers[0]).start();
new Thread(philosophers[1]).start();
new Thread(philosophers[2]).start();
new Thread(philosophers[3]).start();
new Thread(philosophers[4]).start();
//f1.add(obj0);
f1.add(obj);
f1.validate();
}
public void initializePos(){
chopstick=new Chopstick[5];
chopstick[0]=new Chopstick(0,270,330);
chopstick[1]=new Chopstick(1,200,200);
chopstick[2]=new Chopstick(2,350,120);
chopstick[3]=new Chopstick(3,500,210);
chopstick[4]=new Chopstick(4,430,330);
}
public void initializePhilosopher(){
philosophers = new Philosopher[5];
philosophers[0]=new Philosopher(0,chopstick[0],chopstick[4]);
philosophers[1]=new Philosopher(1,chopstick[1],chopstick[0]);
philosophers[2]=new Philosopher(2,chopstick[2],chopstick[1]);
philosophers[3]=new Philosopher(3,chopstick[3],chopstick[2]);
philosophers[4]=new Philosopher(4,chopstick[4],chopstick[3]);
}
public static void main(String args[]){
new App();
}
}
Class Chopstick
import java.util.concurrent.locks.ReentrantLock;
public class Chopstick extends ReentrantLock {
private int id;
private static int xpos[]=new int[5];
private static int ypos[]=new int[5];
public Chopstick(){
}
public Chopstick(int id, int xpos,int ypos){
this.id=id;
this.xpos[id]=xpos;
this.ypos[id]=ypos;
}
public static void setAxis(int id,int x,int y){
xpos[id]=x;
ypos[id]=y;
}
public int getXpos(int id){
return xpos[id];
}
public int getYpos(int id){
return ypos[id];
}
}
Class Philosopher
import java.util.Random;
import java.util.concurrent.locks.Lock;
public class Philosopher implements Runnable {
private int id;
private Lock leftChopstick;
private Lock rightChopstick;
private static int noMeals[]=new int [5];
private Random timeGenerator = new Random();
private static int state[]=new int[5];
private static int quote[]=new int[5];
private Chopstick chopstick=new Chopstick();
public Philosopher(){
}
public Philosopher(int id,Lock leftChopstick, Lock rightChopstick){
this.id=id;
this.leftChopstick=leftChopstick;
this.rightChopstick=rightChopstick;
}
public void run(){
try {
while (true) {
think();
pickUpChopstick(leftChopstick,rightChopstick);
eat();
putDownChopsticks();
}
} catch (InterruptedException e) {
System.out.println("Philosopher " + id + " was interrupted.\n");
}
}
private void think() throws InterruptedException {
System.out.println("Philosopher " + id + " is thinking.\n");
System.out.flush();
if(id==0){
state[0]=4;
}
if(id==1){
state[1]=4;
}
if(id==2){
state[2]=4;
}
if(id==3){
state[3]=4;
}
if(id==4){
state[4]=4;
}//
Thread.sleep (1000+timeGenerator.nextInt(1000));
}
private void pickUpChopstick(Lock leftChopstick,Lock rightChopstick){
while(true){
boolean lc=false;
boolean rc=false;
try{
lc=leftChopstick.tryLock();
rc=rightChopstick.tryLock();
}finally{
if(lc&&rc){
System.out.println("Philosopher " + id + " Pick Up both Chop Stick.\n");
quote[id]=3;
return;
}
if(lc){
System.out.println("Philosopher " + id + " Pick Up leftChop Stick And RightChop Stick is Busy.\n");
leftChopstick.unlock();
System.out.println("Philosopher " + id + " Put Down leftChop Stick.\n");
quote[id]=1;
}
if(rc){
System.out.println("Philosopher " + id + " Pick Up RightChop Stick And lefttChop Stick is Busy.\n");
rightChopstick.unlock();
System.out.println("Philosopher " + id + " Put Down RightChop Stick.\n");
quote[id]=2;
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void eat() throws InterruptedException {
Thread.sleep(50);
quote[id]=0;
increment(id);
System.out.println("Philosopher " + id + " is eating.\n");
if(id==0){
chopstick.setAxis(0, 340, 350);
chopstick.setAxis(4, 380, 350);
state[0]=0;
}
if(id==1){
chopstick.setAxis(0, 220, 290);
chopstick.setAxis(1, 200, 250);
state[1]=0;
}
if(id==2){
chopstick.setAxis(1, 240, 150);
chopstick.setAxis(2, 280, 120);
state[2]=0;
}
if(id==3){
chopstick.setAxis(2, 430, 120);
chopstick.setAxis(3, 500, 150);
state[3]=0;
}
if(id==4){
chopstick.setAxis(3, 530, 270);
chopstick.setAxis(4, 490, 315);
state[4]=0;
}
System.out.flush();
Thread.sleep (400);
//
if(id==0){
state[0]=1;
}
if(id==1){
state[1]=1;
}
if(id==2){
state[2]=1;
}
if(id==3){
state[3]=1;
}
if(id==4){
state[4]=1;
}
//
Thread.sleep (400);
//
if(id==0){
state[0]=2;
}
if(id==1){
state[1]=2;
}
if(id==2){
state[2]=2;
}
if(id==3){
state[3]=2;
}
if(id==4){
state[4]=2;
}//
//
Thread.sleep (400);
//
if(id==0){
state[0]=3;
}
if(id==1){
state[1]=3;
}
if(id==2){
state[2]=3;
}
if(id==3){
state[3]=3;
}
if(id==4){
state[4]=3;
}//
Thread.sleep (200+timeGenerator.nextInt(200));
}
private void putDownChopsticks() {
leftChopstick.unlock();
rightChopstick.unlock();
System.out.println("Philosopher " + id + " put down Both ChopStick\n");
if(id==0){
chopstick.setAxis(0, 270, 330);
chopstick.setAxis(4, 430, 330);
state[0]=4;
}
if(id==1){
chopstick.setAxis(0, 270, 330);
chopstick.setAxis(1, 200, 200);
state[1]=4;
}
if(id==2){
chopstick.setAxis(1, 200, 200);
chopstick.setAxis(2, 350, 120);
state[2]=4;
}
if(id==3){
chopstick.setAxis(2, 350, 120);
chopstick.setAxis(3, 500, 210);
state[3]=4;
}
if(id==4){
chopstick.setAxis(3, 500, 210);
chopstick.setAxis(4, 430, 330);
state[4]=4;
}
}
public int getS(int id){
return state[id];
}
public void setS(int id,int s){
state[id]=s;
}
public void increment(int id){
noMeals[id]++;
}
public int getNoMeals(int id){
return noMeals[id];
}
public int getQuote(int id){
return quote[id];
}
}
DISPLAY CLASS
import java.awt.*;
import javax.swing.*;
public class Display extends JPanel implements Runnable {
Image bg;
Image s0,s1,s2,s3,s4;
Image plate[]=new Image[5];
Toolkit t;
Image state1,state2;
Font font;
Chopstick chopstick;
Philosopher philosopher;
public Display(){
t=Toolkit.getDefaultToolkit();
font = new Font("Serif", Font.PLAIN, 20);
bg=t.getImage("image//BG1.png");
s0=t.getImage("image//ts0.png");
s1=t.getImage("image//ts1.png");
s2=t.getImage("image//ts2.png");
s3=t.getImage("image//ts3.png");
s4=t.getImage("image//ts4.png");
state1=t.getImage("image//eating.jpg");
state2=t.getImage("image//thinking.jpg");
plate[0]=t.getImage("image//p0.png");
plate[1]=t.getImage("image//p2.png");
plate[2]=t.getImage("image//p3.png");
plate[3]=t.getImage("image//p4.png");
plate[4]=t.getImage("image//p5.png");
chopstick=new Chopstick();
philosopher=new Philosopher();
}
public void run(){
while(true){
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void paint(Graphics g){
super.paint(g);
//this.setBackground(Color.white);
g.drawImage(bg,0 , 0, 800, 600, null);
drawP(g);
drawP0(g);
drawP1(g);
drawP2(g);
drawP3(g);
drawP4(g);
draw0(g);
draw1(g);
draw2(g);
draw3(g);
draw4(g);
drawS0(g);
drawS1(g);
drawS2(g);
drawS3(g);
drawS4(g);
}
public void draw0(Graphics g){
g.drawImage(s0,chopstick.getXpos(0), chopstick.getYpos(0), 100, 100, null);
}
public void draw1(Graphics g){
g.drawImage(s1,chopstick.getXpos(1) , chopstick.getYpos(1), 100, 100, null);
}public void draw2(Graphics g){
g.drawImage(s2,chopstick.getXpos(2), chopstick.getYpos(2), 100, 100, null);
}public void draw3(Graphics g){
g.drawImage(s3,chopstick.getXpos(3), chopstick.getYpos(3), 100, 100, null);
}public void draw4(Graphics g){
g.drawImage(s4,chopstick.getXpos(4), chopstick.getYpos(4), 100, 100, null);
}
public void drawP0(Graphics g){
g.drawImage(plate[philosopher.getS(0)], 355,340, 100, 100, null);
}
public void drawP1(Graphics g){
g.drawImage(plate[philosopher.getS(1)], 210,275, 100, 100, null);
}
public void drawP2(Graphics g){
g.drawImage(plate[philosopher.getS(2)], 275,145, 100, 100, null);
}
public void drawP3(Graphics g){
g.drawImage(plate[philosopher.getS(3)], 445,145, 100, 100, null);
}
public void drawP4(Graphics g){
g.drawImage(plate[philosopher.getS(4)], 488,285, 100, 100, null);
}
public void drawP(Graphics g){
g.drawImage(plate[0], 355,340, 100, 100, null);
g.drawImage(plate[0], 210,275, 100, 100, null);
g.drawImage(plate[0], 275,145, 100, 100, null);
g.drawImage(plate[0], 445,145, 100, 100, null);
g.drawImage(plate[0], 488,285, 100, 100, null);
}
//
public void drawS0(Graphics g){
g.setFont(font);
//getNoMeals
g.setColor(Color.RED);
g.drawString("No Of Meals="+Integer.toString(philosopher.getNoMeals(0)), 920, 60);
g.setColor(Color.BLACK);
if(philosopher.getS(0)==4){
g.drawImage(state2,800 , 0, 120, 120, null);
g.drawString("Thinking", 920, 30);
}else{
g.drawImage(state1,800 , 0, 120, 120, null);
g.drawString("Eating", 920, 30);
}
}
public void drawS1(Graphics g){
g.setFont(font);
g.setColor(Color.RED);
g.drawString("No Of Meals="+Integer.toString(philosopher.getNoMeals(1)), 920, 180);
g.setColor(Color.BLACK);
if(philosopher.getS(1)==4){
g.drawImage(state2,800 , 120, 120, 120, null);
g.drawString("Thinking", 920, 150);
}else{
g.drawImage(state1,800 , 120, 120, 120, null);
g.drawString("Eating", 920, 150);
}
}
public void drawS2(Graphics g){
g.setFont(font);
g.setColor(Color.RED);
g.drawString("No Of Meals="+Integer.toString(philosopher.getNoMeals(2)), 920, 300);
g.setColor(Color.BLACK);
if(philosopher.getS(2)==4){
g.drawImage(state2,800 , 240, 120, 120, null);
g.drawString("Thinking", 920, 270);
}else{
g.drawImage(state1,800 , 240, 120, 120, null);
g.drawString("Eating", 920, 270);
}
}public void drawS3(Graphics g){
g.setFont(font);
g.setColor(Color.RED);
g.drawString("No Of Meals="+Integer.toString(philosopher.getNoMeals(3)), 920, 420);
g.setColor(Color.BLACK);
if(philosopher.getS(3)==4){
g.drawImage(state2,800, 360, 120, 120, null);
g.drawString("Thinking", 920, 390);
}else{
g.drawImage(state1,800, 360, 120, 120, null);
g.drawString("Eating", 920, 390);
}
}public void drawS4(Graphics g){
g.setFont(font);
g.setColor(Color.RED);
g.drawString("No Of Meals="+Integer.toString(philosopher.getNoMeals(4)), 920, 540);
g.setColor(Color.BLACK);
if(philosopher.getS(4)==4){
g.drawImage(state2,800, 480, 120, 120, null);
g.drawString("Thinking", 920, 510);
}else{
g.drawImage(state1,800, 480, 120, 120, null);
g.drawString("Eating", 920, 510);
}
}
public void drawQuote(Graphics g){
if(philosopher.getQuote(0)==1){
}
if(philosopher.getQuote(0)==2){
}if(philosopher.getQuote(0)==3){
}
}
}
thanks for your great application
ReplyDeletebut what's the display class?
Display class is for graphics
DeletePlease whats img?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteCan you share the images please and an amazing application helped me alot with my OS project
ReplyDeletePlease I need the images .Upload it
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteimages pls
ReplyDeletesir please upload the image for learning and understand ty
ReplyDeleteHi bro, I´ve sent you a message . Can you help me please?
ReplyDelete