Skip to content

Commit 47a6680

Browse files
committed
Dinning Philosopher problem
1 parent 4b6ede1 commit 47a6680

File tree

11 files changed

+176
-0
lines changed

11 files changed

+176
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.ashfaq.dev.problems.DiningPhilosophers;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
public class App {
7+
8+
public static void main(String[] args) throws InterruptedException {
9+
10+
ExecutorService executorService = null;
11+
Philosopher[] philosophers = null;
12+
13+
try{
14+
15+
philosophers = new Philosopher[Constants.NUM_PHILOSOPHERS];
16+
Chopsticks[] chopSticks = new Chopsticks[Constants.NUM_PHILOSOPHERS];
17+
18+
for(int i=0;i<Constants.NUM_CHOPSTICKS;i++){
19+
chopSticks[i] = new Chopsticks(i);
20+
}
21+
22+
executorService = Executors.newFixedThreadPool(Constants.NUM_PHILOSOPHERS);
23+
24+
for(int i=0;i<Constants.NUM_PHILOSOPHERS;i++){
25+
philosophers[i] = new Philosopher(i, chopSticks[i], chopSticks[(i+1) % Constants.NUM_PHILOSOPHERS]);
26+
executorService.execute(philosophers[i]);
27+
}
28+
29+
Thread.sleep(Constants.Simulation_Running_Time);
30+
31+
for(Philosopher philosopher : philosophers){
32+
philosopher.setFull(true);
33+
}
34+
}finally{
35+
36+
executorService.shutdown();
37+
38+
while(!executorService.isTerminated()){
39+
Thread.sleep(1000);
40+
}
41+
42+
for(Philosopher philosopher : philosophers ){
43+
System.out.println(philosopher+" eat #"+philosopher.getEatingCounter());
44+
}
45+
46+
}
47+
48+
}
49+
}
50+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.ashfaq.dev.problems.DiningPhilosophers;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.locks.Lock;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.concurrent.locks.Lock;
9+
import java.util.concurrent.locks.ReentrantLock;
10+
11+
public class Chopsticks {
12+
13+
private Lock lock;
14+
private int id;
15+
16+
public Chopsticks(int id){
17+
this.id = id;
18+
this.lock = new ReentrantLock();
19+
}
20+
21+
public boolean pickUp(Philosopher philosopher, State state) throws InterruptedException{
22+
23+
if( this.lock.tryLock(10, TimeUnit.MILLISECONDS)){
24+
System.out.println(philosopher+" picked up "+state.toString()+" "+this);
25+
return true;
26+
}
27+
28+
return false;
29+
}
30+
31+
public void putDown(Philosopher philosopher, State state) {
32+
this.lock.unlock();
33+
System.out.println(philosopher+" put down "+this);
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return "Chopstick-"+this.id;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.ashfaq.dev.problems.DiningPhilosophers;
2+
3+
public class Constants {
4+
5+
public static final int NUM_PHILOSOPHERS = 5;
6+
public static final int NUM_CHOPSTICKS = 5;
7+
public static final int Simulation_Running_Time = 1000;
8+
9+
10+
11+
12+
private Constants() {
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.ashfaq.dev.problems.DiningPhilosophers;
2+
3+
import java.util.Random;
4+
5+
public class Philosopher implements Runnable {
6+
7+
private int id;
8+
private Chopsticks leftChopStick;
9+
private Chopsticks rightChopStick;
10+
private volatile boolean isFull = false;
11+
private Random random;
12+
private int eatingCounter;
13+
14+
public Philosopher(int id, Chopsticks leftChopStick, Chopsticks rightChopStick){
15+
this.id = id;
16+
this.leftChopStick = leftChopStick;
17+
this.rightChopStick = rightChopStick;
18+
this.random = new Random();
19+
}
20+
21+
@Override
22+
public void run() {
23+
24+
try{
25+
26+
while( !isFull ){
27+
28+
think();
29+
30+
if( leftChopStick.pickUp(this, State.LEFT) ){
31+
if( rightChopStick.pickUp(this, State.RIGHT)){
32+
eat();
33+
rightChopStick.putDown(this, State.RIGHT);
34+
}
35+
36+
leftChopStick.putDown(this, State.LEFT);
37+
}
38+
}
39+
}catch(Exception e){
40+
e.printStackTrace();
41+
}
42+
}
43+
44+
private void think() throws InterruptedException {
45+
System.out.println(this+" is thinking...");
46+
Thread.sleep(this.random.nextInt(1000));
47+
}
48+
49+
private void eat() throws InterruptedException {
50+
System.out.println(this+" is eating...");
51+
this.eatingCounter++;
52+
Thread.sleep(this.random.nextInt(1000));
53+
}
54+
55+
public int getEatingCounter(){
56+
return this.eatingCounter;
57+
}
58+
59+
public void setFull(boolean isFull){
60+
this.isFull = isFull;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "Philosopher-"+this.id;
66+
}
67+
}
68+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.ashfaq.dev.problems.DiningPhilosophers;
2+
public enum State {
3+
LEFT, RIGHT;
4+
}

0 commit comments

Comments
 (0)