-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBread.java
More file actions
102 lines (90 loc) · 2.99 KB
/
Copy pathBread.java
File metadata and controls
102 lines (90 loc) · 2.99 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
/*
* The baker has a lot of bread, which is sold by n sales people.
* Each customer enters the store in order to take a number,
* and waits for the number to be called,
* and when a salesperson is free, the next number is called in order.
* Using two integer variables to record the current sign value and call value,
* design an algorithm to synchronize sales and customers.
*/
public class Bread {
public static void main(String[] args) {
Semaphores semaphores = new Semaphores();
Thread thread_consumer_1 = new Thread(new Consumer(semaphores, 1));
Thread thread_consumer_2 = new Thread(new Consumer(semaphores, 2));
Thread thread_seller_1 = new Thread(new Seller(semaphores, 1));
Thread thread_seller_2 = new Thread(new Seller(semaphores, 2));
thread_consumer_1.start();
thread_consumer_2.start();
thread_seller_1.start();
thread_seller_2.start();
}
}
class Semaphores {
public int i = 0;
public int j = 0;
public boolean mutex_i = true;
public boolean mutex_j = true;
public synchronized boolean P_mutex_i() {
if (mutex_i) {
mutex_i = false;
return true;
} else {
return false;
}
}
public synchronized boolean P_mutex_j() {
if (mutex_j) {
mutex_j = false;
return true;
} else {
return false;
}
}
public synchronized void V_mutex_i() {
mutex_i = true;
}
public synchronized void V_mutex_j() {
mutex_j = true;
}
}
class Consumer implements Runnable {
public Semaphores semaphores;
public int rank;
public Consumer(Semaphores semaphores, int rank) {
this.semaphores = semaphores;
this.rank = rank;
}
public void run() {
while (true) {
System.out.println("Consumer " + rank + " enter the bakery.");
while (!semaphores.P_mutex_i()) {
}
System.out.println("Consumer " + rank + " get the number " + semaphores.i + ".");
semaphores.i++;
semaphores.V_mutex_i();
System.out.println("Consumer " + rank + " is waiting for buying bread.");
}
}
}
class Seller implements Runnable {
public Semaphores semaphores;
public int rank;
public Seller(Semaphores semaphores, int rank) {
this.semaphores = semaphores;
this.rank = rank;
}
public void run() {
while (true) {
while (!semaphores.P_mutex_j()) {
}
if (semaphores.j < semaphores.i) {
System.out.println("Seller " + rank + " calls number " + semaphores.j + ".");
semaphores.j++;
semaphores.V_mutex_j();
} else {
semaphores.V_mutex_j();
System.out.println("Seller " + rank + " is free.");
}
}
}
}