-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTampon.java
More file actions
44 lines (38 loc) · 807 Bytes
/
Tampon.java
File metadata and controls
44 lines (38 loc) · 807 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
44
package muprodmucons;
import java.util.ArrayList;
public class Tampon<E> {
public ArrayList<E> objets = new ArrayList<E>();
public int max;
public Tampon (int maximum) {
max = maximum;
}
public synchronized E retrait() {
while (objets.isEmpty()) {
try {
wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("Tampon : [");
for (int k=0; k<objets.size()-1; k++) {
System.out.print(objets.get(k) + ",");
}
System.out.println(objets.get(objets.size()-1) + "]");
E temp = objets.get(0);
objets.remove(0);
return temp;
}
public synchronized int depot(E obj) {
if (objets.size() < max) {
objets.add(obj);
notifyAll();
return 0;
}
else {
System.out.println("Le tampon est plein.");
return 1;
}
}
}