-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSilo.java
More file actions
43 lines (38 loc) · 1016 Bytes
/
Copy pathSilo.java
File metadata and controls
43 lines (38 loc) · 1016 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
37
38
39
40
41
42
43
package ds.silo;
public class Silo {
//make the producers wait when it exceedes to 20 unit
private final String what;
private int unit;
public Silo(String what) {
super();
this.what = what;
}
public synchronized void get() {
// we don't have any product at hand
while (unit == 0) {
try {
wait();
} catch (Exception e) {
e.printStackTrace();
}
}
unit--;
System.out.println(Thread.currentThread().getName() + " getting " + this.what);
notify();
}
public synchronized void put() {
while (unit == 20) {
try {
wait();
} catch (Exception e) {
e.printStackTrace();
}
}
unit++;
System.out.println(Thread.currentThread().getName() + " putting " + this.what);
notify();
}
public String toString() {
return what + ":" + unit;
}
}