-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeller.java
More file actions
38 lines (29 loc) · 855 Bytes
/
Teller.java
File metadata and controls
38 lines (29 loc) · 855 Bytes
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
import java.util.concurrent.Semaphore;
public class Teller extends Thread {
int numberOfTeller;
int counter;
public Teller(int numberOfTeller) {
this.numberOfTeller = numberOfTeller;
this.counter = 0;
}
Semaphore tellerSemaphore = new Semaphore(numberOfTeller, true);
public void tellerSimulator() throws InterruptedException {
while (!Customer.customerQueue.isEmpty()) {
if (tellerSemaphore.availablePermits() == 0) {
Thread.sleep(100);
tellerSemaphore.release();
} else {
tellerSemaphore.acquire();
System.out.println(Customer.customerQueue.poll() + " starts being served");
}
}
}
public void run() {
try {
tellerSimulator();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}