-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeadlockDiningPhilosophersDemo.java
95 lines (86 loc) · 2.25 KB
/
DeadlockDiningPhilosophersDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.*;
import java.util.concurrent.*;
class Chopstick{
private boolean taken = false;
public synchronized void take() throws InterruptedException{
while(taken)
wait();
taken = true;
}
public synchronized void drop(){
taken = false;
notifyAll();
}
}
class Philosopher implements Runnable{
private Chopstick left;
private Chopstick right;
private final int id;
private final int ponderFactor;
private Random rand = new Random(47);
private void pause() throws InterruptedException{
if(ponderFactor == 0)
return;
TimeUnit.MILLISECONDS.sleep(rand.nextInt(ponderFactor*250));
}
public Philosopher(Chopstick left, Chopstick right, int ident, int ponder){
this.left = left;
this.right = right;
id = ident;
ponderFactor = ponder;
}
public void run(){
try{
while(!Thread.interrupted()){
System.out.println(this + " thinking...");
pause();
System.out.println(this + " grabbing right");
right.take();
System.out.println(this + " grabbing left");
left.take();
System.out.println(this + " eating");
pause();
right.drop();
left.drop();
}
}catch(InterruptedException e){
System.out.println(this + " " + "exiting via interrupt.");
}
}
public String toString(){
return "Philosopher " + id;
}
}
public class DeaklockDiningPhilosophersDemo{
public static void main(String[] args) throws Exception{
int ponder = 5;
if(args.length > 0)
ponder = Integer.parseInt(args[0]);
int size = 5;
if(args.length > 1)
size = Integer.parseInt(args[1]);
ExecutorService exec = Executors.newCachedThreadPool();
Chopstick[] sticks = new Chopstick[size];
for(int i = 0;i < size;i++)
sticks[i] = new Chopstick();
/*
to prevent deadlock, see following comment
*/
for(int i = 0;i < size;i++)
exec.execute(new Philosopher(sticks[i], sticks[(i+1)%size],i,ponder));
/*
for(int i = 0; i < size; i++)
if(i < (size-1))
exec.execute(new Philosopher(sticks[i], sticks[i+1], i, ponder));
else
exec.execute(new Philosopher(sticks[0], sticks[i], i, ponder));
*/
if(args.length == 3 && args[2].equals("timeout"))
TimeUnit.SECONDS.sleep(5);
else{
System.out.println("Press 'Enter' to quit.");
System.in.read();
}
exec.shutdownNow();
}
}