-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBankTellerSimulation.java
More file actions
249 lines (184 loc) · 6.54 KB
/
BankTellerSimulation.java
File metadata and controls
249 lines (184 loc) · 6.54 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
249
package concurrency;
import java.util.concurrent.*;
import java.util.*;
/**
* RUN:
* javac concurrency/BankTellerSimulation.java && java concurrency.BankTellerSimulation
*
* OUTPUT:
*
*/
public class BankTellerSimulation {
static final int MAX_LINE_SIZE = 50;
static final int ADJUSTMENT_PERIOD = 1000;
public static void main(String[] args) throws Exception {
ExecutorService exec =Executors.newCachedThreadPool();
CustomerLine customers = new CustomerLine(MAX_LINE_SIZE);
exec.execute(new CustomerGenerator(customers));
exec.execute(new TellerManager(exec, customers, ADJUSTMENT_PERIOD));
if (args.length > 0) {
TimeUnit.SECONDS.sleep(new Integer(args[0]));
}
else {
System.out.println("Press 'Enter' to quit");
System.in.read();
}
exec.shutdownNow();
}
}
// -----------------------------------------------------------------------------
class Customer {
private final int serviceTime;
public Customer(int tm) { serviceTime = tm; }
public int getServiceTime() { return serviceTime; }
public String toString() { return String.format("[%1$d]", serviceTime); }
}
// -----------------------------------------------------------------------------
class CustomerLine extends ArrayBlockingQueue<Customer> {
public CustomerLine(int maxLineSize) {
super(maxLineSize);
}
public String toString() {
if (this.size() == 0) {
return "[empty]";
}
StringBuilder sb = new StringBuilder();
for (Customer customer : this) {
sb.append(customer);
}
return sb.toString();
}
}
// -----------------------------------------------------------------------------
class CustomerGenerator implements Runnable {
private CustomerLine customers;
private static Random rand = new Random(47);
public CustomerGenerator(CustomerLine customers) {
this.customers = customers;
}
public void run() {
try {
while (! Thread.interrupted()) {
TimeUnit.MILLISECONDS.sleep(rand.nextInt(300));
customers.put(new Customer(rand.nextInt(1000)));
}
}
catch (InterruptedException e) {
System.out.println("CustomerGenerator interrupted");
}
System.out.println("CustomerGenerator terminating");
}
}
// -----------------------------------------------------------------------------
class Teller implements Runnable, Comparable<Teller> {
private static int counter = 0;
private final int id = counter++;
private int customerServed = 0;
private CustomerLine customers;
private boolean servingCustomerLine = true;
public Teller(CustomerLine customers) {
this.customers = customers;
}
public void run() {
try {
while (! Thread.interrupted()) {
Customer customer = customers.take();
TimeUnit.MILLISECONDS.sleep(customer.getServiceTime());
synchronized(this) {
customerServed++;
while (! servingCustomerLine) {
wait();
}
}
}
}
catch (InterruptedException e) {
System.out.println(this + " interrupted");
}
System.out.println(this + " terminating");
}
public synchronized void doSomethingElse() {
customerServed = 0;
servingCustomerLine = false;
}
public synchronized void serveCustomerLine() {
assert (! servingCustomerLine): "serving now: " + this;
servingCustomerLine = true;
notifyAll();
}
public String toString() { return "Teller " + id + " "; }
public String shortString() { return "T" + id; }
public synchronized int compareTo(Teller other) {
return (customerServed < other.customerServed) ? -1 :
(customerServed == other.customerServed ? 0 : 1);
}
}
// -----------------------------------------------------------------------------
class TellerManager implements Runnable {
private ExecutorService exec;
private CustomerLine customers;
private PriorityQueue<Teller> workingTellers = new PriorityQueue<Teller>();
private Queue<Teller> tellersDoingOtherThings = new LinkedList<Teller>();
private int adjustmentPeriod;
private static Random rand = new Random(47);
public TellerManager(ExecutorService exec, CustomerLine customers, int adjustmentPeriod) {
this.exec = exec;
this.customers = customers;
this.adjustmentPeriod = adjustmentPeriod;
Teller teller = new Teller(customers);
exec.execute(teller);
workingTellers.add(teller);
}
public void adjustTellerNumber()
{
if (customers.size() / workingTellers.size() > 2)
{
// pass customer to @free@ teller
if (tellersDoingOtherThings.size() > 0)
{
Teller teller = tellersDoingOtherThings.remove();
teller.serveCustomerLine();
workingTellers.offer(teller);
return;
}
// hire new teller if there is a big number of customers
Teller teller = new Teller(customers);
exec.execute(teller);
workingTellers.add(teller);
return;
}
if (workingTellers.size() > 1 && customers.size() / workingTellers.size() < 2) {
reassignOneTeller();
}
if (customers.size() == 0) {
while (workingTellers.size() > 1) {
reassignOneTeller();
}
}
}
private void reassignOneTeller() {
Teller teller = workingTellers.poll();
teller.doSomethingElse();
tellersDoingOtherThings.offer(teller);
}
public void run() {
try {
while (! Thread.interrupted()) {
TimeUnit.MILLISECONDS.sleep(adjustmentPeriod);
adjustTellerNumber();
System.out.print(customers + " { ");
for (Teller teller : workingTellers) {
System.out.print(teller.shortString() + " ");
}
System.out.println(" }");
}
}
catch(InterruptedException w) {
System.out.println(this + " interrupted");
}
System.out.println(this + " terminating");
}
public String toString() {
return "TellerManager ";
}
}