-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBarber.java
More file actions
35 lines (27 loc) · 915 Bytes
/
Barber.java
File metadata and controls
35 lines (27 loc) · 915 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
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Random;
public class Barber implements Runnable{
private AtomicInteger spaces;
private Semaphore bavailable;
private Semaphore cavailable;
private Random ran = new Random();
public Barber(AtomicInteger spaces, Semaphore bavailable, Semaphore cavailable){
this.spaces = spaces;
this.bavailable = bavailable;
this.cavailable = cavailable;
}
@Override
public void run(){
while(true){
try {
cavailable.acquire();
// Space freed up in waiting area
System.out.println("Customer getting hair cut");
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 10000 + 1000)); // Sleep to imitate length of time to cut hair
System.out.println("Customer Pays and leaves");
bavailable.release();
} catch (InterruptedException e){}
}
}
}