-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomer.java
More file actions
50 lines (37 loc) · 1.23 KB
/
Copy pathcustomer.java
File metadata and controls
50 lines (37 loc) · 1.23 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
package sleeping_barber;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.Semaphore;
public class customer implements Runnable
{
private BlockingDeque<String> waiting_list;
private Semaphore Barbers_ready;
private Semaphore Customers_ready;
private String Customer_name;
// Create self instance of customer class
public customer(BlockingDeque<String> waiting_list,Semaphore Barbers_ready, Semaphore Customers_ready, String Customer_name)
{
this.waiting_list = waiting_list;
this.Barbers_ready = Barbers_ready;
this.Customers_ready = Customers_ready;
this.Customer_name = Customer_name;
}
// Run method create for the runnable
public void run()
{
try
{
Customers_ready.release(); // release the lock on the customer when customer is ready for the hair cut
waiting_list.put(Customer_name);
if (Barbers_ready.hasQueuedThreads()) //waiting_list.size() > 1
{
System.out.println("No barber is free " + Customer_name + " is in waiting area\n");
}
Barbers_ready.acquire(); // acquire the lock on the barber if he is free to cut the customer the hair
// System.out.println("hello");
}
catch(Exception e)
{
System.out.println(e);
}
}
}