Repository navigation
Expand file tree
/
Copy pathSimulationSolved.java
More file actions
248 lines (199 loc) · 6.78 KB
/
Copy pathSimulationSolved.java
File metadata and controls
248 lines (199 loc) · 6.78 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
class ServiceStation {
ArrayList<Car> waitingQueue,allCars;
Semaphore mutex, empty, full;
pumpsemaphore pumps;
public final int totalCars;
public AtomicInteger carsServicedCount;
//AtomicInteger workingPumps;
ServiceStation(int pumpsNumber, int queueSize, int totatlCars) {
allCars = new ArrayList<>();
waitingQueue = new ArrayList<>(/*queueSize*/);
mutex = new Semaphore(1);
full = new Semaphore(0);
empty = new Semaphore(queueSize);
pumps = new pumpsemaphore(pumpsNumber);
this.totalCars = totatlCars;
this.carsServicedCount = new AtomicInteger(0);
//this.workingPumps= new AtomicInteger(pumpsNumber);
}
}
class Pump implements Runnable {
private int id;
private ServiceStation station;
//private Random rand;
public Pump(int id, ServiceStation station) {
this.id = id;
this.station = station;
//this.rand = new Random();
}
@Override
public void run() {
while (station.carsServicedCount.get() < station.totalCars) {
Car c;
try {
station.full.P();
station.mutex.P();
c = station.waitingQueue.remove(0);
station.carsServicedCount.incrementAndGet();
station.mutex.V();
station.empty.V();
if (c == null) {
continue;
}
System.out.println("Pump " + id + ": " + c.getId() + " Occupied");
// station.workingPumps.decrementAndGet();
station.pumps.P();
System.out.println("Pump " + id + ": " + c.getId() + " login");
System.out.println("Pump " + id + ": " + c.getId() + " begins service at Bay " + id);
Thread.sleep(5000);
System.out.println("Pump " + id + ": " + c.getId() + " finishes service");
//station.workingPumps.incrementAndGet();
station.pumps.V();
System.out.println("Pump " + id + ": Bay " + id + " is free");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
class Semaphore {
protected int count;
protected Semaphore() {
count = 0;
}
public Semaphore(int count) {
if (count < 0) throw new IllegalArgumentException();
this.count = count;
}
public synchronized void P() {
count--;
if (count < 0) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public synchronized void V() {
count++;
if (count <= 0)
notify();
}
public synchronized int getCount() {
return count;
}
}
class pumpsemaphore extends Semaphore{
pumpsemaphore(int count){
super(count);
}
@Override
public synchronized void P(){
if (count < 0) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public synchronized boolean tryAccquire() {
if (count > 0) {
count--;
return true; // got a permit
} else {
return false; // none available
}
}
}
class Car implements Runnable {
private String id;
private ServiceStation station;
Car(String id, ServiceStation station) {
this.id = id;
this.station = station;
}
@Override
public void run() {
System.out.println(id + " arrived");
station.mutex.P();
station.allCars.add(this);
station.mutex.V();
station.empty.P();
station.mutex.P();
if(!station.allCars.isEmpty())
station.waitingQueue.add(station.allCars.remove(0));
if (!station.pumps.tryAccquire()) {
System.out.println(this.id + " arrived and waiting");
station.pumps.P();
}
station.mutex.V();
station.full.V();
}
public String getId() {
return id;
}
}
public class SimulationSolved {
public static boolean isInteger(String s) {
if (s == null || s.isEmpty()) return false;
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static void main(String[] args) throws InterruptedException {
String input;
Scanner in = new Scanner(System.in);
String[] msg = new String[]{"Waiting area capacity: ", "Number of service bays (pumps): ", "Cars arriving (order): "};
int idx = 0, area = 0, NumPumps = 0, NumCars = 0;
String[] names = {};
// Taking input and validations are made
while (idx < 3) {
if (idx == 0) {
System.out.println(msg[idx]);
input = in.nextLine();
if (isInteger(input) && Integer.parseInt(input) <= 10 && Integer.parseInt(input) >= 1) {
idx++;
area = Integer.parseInt(input);
} else System.out.println("Invalid input!");
} else if (idx == 1) {
System.out.println(msg[idx]);
input = in.nextLine();
if (isInteger(input) && Integer.parseInt(input) >= 1) {
idx++;
NumPumps = Integer.parseInt(input);
} else System.out.println("Invalid input!");
} else {
System.out.println(msg[idx]);
input = in.nextLine();
names = input.split(",");
NumCars = names.length;
idx++;
}
}
ServiceStation station = new ServiceStation(NumPumps, area, NumCars);
Thread[] Pumps = new Thread[NumPumps];
Thread[] Cars = new Thread[NumCars];
for (int i = 0; i < NumPumps; i++) {
Pumps[i] = new Thread(new Pump(i + 1, station));
Pumps[i].start();
}
for (int i = 0; i < NumCars; i++) {
Car car = new Car(names[i], station);
Cars[i] = new Thread(car);
//station.allCars.add(car);
// Thread.sleep(1);
}
for (int i = 0; i < NumCars; i++) {
Cars[i].start();
Thread.sleep(1500);
}
}
}