-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCustomer.java
More file actions
36 lines (29 loc) · 839 Bytes
/
Copy pathCustomer.java
File metadata and controls
36 lines (29 loc) · 839 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
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Random;
public class Customer implements Runnable{
private AtomicInteger spaces;
private Semaphore bavailable;
private Semaphore cavailable;
private Random ran = new Random();
public Customer(AtomicInteger spaces, Semaphore bavailable, Semaphore cavailable){
this.spaces = spaces;
this.bavailable = bavailable;
this.cavailable = cavailable;
}
@Override
public void run(){
try {
cavailable.release();
if(bavailable.hasQueuedThreads()){
spaces.decrementAndGet();
System.out.println("Customer in waiting area");
bavailable.acquire();
spaces.incrementAndGet();
}
else{
bavailable.acquire();
}
} catch (InterruptedException e){}
}
}