-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDinningPhilosopher.java
73 lines (59 loc) · 2.14 KB
/
DinningPhilosopher.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
import java.util.Scanner;
class Philosopher implements Runnable {
// The forks on either side of this Philosopher
private Object leftFork;
private Object rightFork;
public Philosopher(Object leftFork, Object rightFork) {
this.leftFork = leftFork;
this.rightFork = rightFork;
}
private void doAction(String action) throws InterruptedException {
System.out.println(
Thread.currentThread().getName() + " " + action);
Thread.sleep(((int) (Math.random() * 100)));
}
@Override
public void run() {
try {
// thinking
doAction(": Thinking");
synchronized (leftFork) {
doAction(": Picked up left fork");
synchronized (rightFork) {
// eating
doAction(": Picked up right fork - eating");
doAction(": Put down right fork");
}
// Back to thinking
doAction(": Put down left fork. Back to thinking");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}
public class DinningPhilosopher {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
Philosopher[] philosophers = new Philosopher[number];
Object[] forks = new Object[philosophers.length];
for (int i = 0; i < forks.length; i++) {
forks[i] = new Object();
}
for (int i = 0; i < philosophers.length; i++) {
Object leftFork = forks[i];
Object rightFork = forks[(i + 1) % forks.length];
if (i == philosophers.length - 1) {
// The last philosopher picks up the right fork first
philosophers[i] = new Philosopher(rightFork, leftFork);
} else {
philosophers[i] = new Philosopher(leftFork, rightFork);
}
Thread t = new Thread(philosophers[i], "Philosopher " + (i + 1));
t.start();
}
sc.close();
}
}