-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBarberProblem.java
More file actions
121 lines (107 loc) · 3.31 KB
/
Copy pathBarberProblem.java
File metadata and controls
121 lines (107 loc) · 3.31 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
/*
* The barber shop has a barber, a barber chair,
* and several chairs for customers waiting to have their hair cut.
* If there is no customer, the barber will sleep in the barber chair,
* when a customer arrives, the customer must wake the barber,
* if the barber is cutting the hair while the customer comes,
* if there is an empty chair to sit down and wait, otherwise he will leave.
* Implementation of the barber shop synchronization problem.
*/
public class BarberProblem {
public static void main(String[] args) {
Semaphores semaphores = new Semaphores();
Thread barber1 = new Thread(new Barber(semaphores, 1));
Thread barber2 = new Thread(new Barber(semaphores, 2));
Thread customer1 = new Thread(new Customer(semaphores, 1));
Thread customer2 = new Thread(new Customer(semaphores, 2));
Thread customer3 = new Thread(new Customer(semaphores, 3));
barber1.start();
barber2.start();
customer1.start();
customer2.start();
customer3.start();
}
}
class Semaphores {
public int waiting = 0;
public int charis = 3;
public int customers = 0;
public int barbers = 0;
public boolean mutex = true;
public synchronized boolean pCustomers() {
if (customers > 0) {
customers--;
return true;
} else {
return false;
}
}
public synchronized boolean pMutex() {
if (mutex) {
mutex = false;
return true;
} else {
return false;
}
}
public synchronized boolean pBarbers() {
if (barbers > 0) {
barbers--;
return true;
} else {
return false;
}
}
public synchronized void vCustomers() {
customers++;
}
public synchronized void vBarbers() {
barbers++;
}
public synchronized void vMutex() {
mutex = true;
}
}
class Barber implements Runnable {
public Semaphores semaphores;
public int rank;
public Barber(Semaphores semaphores, int rank) {
this.semaphores = semaphores;
this.rank = rank;
}
public void run() {
while (true) {
while (!semaphores.pCustomers()) {
}
while (!semaphores.pMutex()) {
}
semaphores.waiting--;
System.out.println("Barber " + rank + " is cutting hair.");
semaphores.vBarbers();
semaphores.vMutex();
}
}
}
class Customer implements Runnable {
public Semaphores semaphores;
public int rank;
public Customer(Semaphores semaphores, int rank) {
this.semaphores = semaphores;
this.rank = rank;
}
public void run() {
while (!semaphores.pMutex()) {
}
if (semaphores.waiting < semaphores.charis) {
semaphores.waiting++;
System.out.println("Customer " + rank + " is waiting.");
semaphores.vCustomers();
semaphores.vMutex();
while (!semaphores.pBarbers()) {
}
} else {
System.out.println("Customer " + rank + " leaves.");
semaphores.vMutex();
}
}
}