-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathDine.java
151 lines (93 loc) · 3.16 KB
/
Dine.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Program for the classic dining philosopher problem in operating system */
package javacodes;
public class Dine {
public static void main(String[] args) {
int x = 10;
Log.msg(String.valueOf(x));
/* Initializing 5 chopsticks */
Chopstick[] chopistics = new Chopstick[5];
for (int i = 0; i < chopistics.length; i++) {
chopistics[i] = new Chopstick("C: " + i);
}
// Initialising five philosophers and their corresponding positions
Philosopher[] philosophers = new Philosopher[5];
philosophers[0] = new Philosopher("P: 0 - ", chopistics[0], chopistics[1]);
philosophers[1] = new Philosopher("P: 1 - ", chopistics[1], chopistics[2]);
philosophers[2] = new Philosopher("P: 2 - ", chopistics[2], chopistics[3]);
philosophers[3] = new Philosopher("P: 3 - ", chopistics[3], chopistics[4]);
philosophers[4] = new Philosopher("P: 4 - ", chopistics[4], chopistics[0]);
for (int i = 0; i < philosophers.length; i++) {
Log.msg("Thred " + i);
Thread t = new Thread(philosophers[i]);
t.start();
}
}
}
//Class for philosophers
class Philosopher extends Thread {
private Chopstick leftChopistick;
private Chopstick rightChopistick;
private String name;
private int state;
public Philosopher(String name, Chopstick left, Chopstick right) {
this.state = 1;
this.name = name;
leftChopistick = left;
rightChopistick = right;
}
public void eat() {
// While eating, every philosopher pics up the left chopstick first and the the right chopstick. If the philosopher gets both the chopsticks
// he eats for 1000 ms and then releases the chopsticks
//Else he waits for chopsticks to be free
if (!leftChopistick.used) {
if (!rightChopistick.used) {
leftChopistick.take();
rightChopistick.take();
Log.msg(name + " : Eat");
Log.Delay(1000);
leftChopistick.release();
rightChopistick.release();
}
}
think();
}
public void think() {
this.state = 1;
Log.msg(name + " : Think");
Log.Delay(1000);
}
public void run() {
for (int i = 0; i <= 10; i++) {
eat();
}
}
}
class Log {
public static void msg(String msg) {
System.out.println(msg);
}
public static void Delay(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException ex) {
}
}
}
//Class for chopstick that provides use and release functionalities to pilosophers
class Chopstick {
public boolean used;
public String name;
public Chopstick(String name) {
this.name = name;
}
//method to reserve the chopstick for use
public synchronized void take() {
Log.msg("Used :: " + name);
this.used = true;
}
//method to release the chopstick for use
public synchronized void release() {
Log.msg("Released :: " + name);
this.used = false;
}
}