Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
192 lines (159 loc) · 5.39 KB
/
Copy pathMain.java
File metadata and controls
192 lines (159 loc) · 5.39 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
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
class ServiceStation {
ArrayList<Car> waitingQueue;
Semaphore mutex, empty, full, pumps;
public final int totalCars;
public AtomicInteger carsServicedCount;
ServiceStation(int pumpsNumber, int queueSize, int totatlCars) {
waitingQueue = new ArrayList<>(queueSize);
mutex = new Semaphore(1);
full = new Semaphore(0);
empty = new Semaphore(queueSize);
pumps = new Semaphore(pumpsNumber);
this.totalCars = totatlCars;
this.carsServicedCount = new AtomicInteger(0);
}
}
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.pumps.P();
System.out.println("Pump " + id + ": " + c.getId() + " login");
System.out.println("Pump " + id + ": " + c.getId() + " begins service at Bay " + id);
Thread.sleep(1000 + rand.nextInt(1000));
System.out.println("Pump " + id + ": " + c.getId() + " finishes service");
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 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.empty.P();
station.mutex.P();
station.waitingQueue.add(this);
System.out.println(id + " entered the queue (waiting area size = " + station.waitingQueue.size() + ")");
station.mutex.V();
station.full.V();
}
public String getId() {
return id;
}
}
public class Main {
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++) {
Cars[i] = new Thread(new Car(names[i], station));
Cars[i].start();
Thread.sleep(300);
}
}
}