TD1: Initial commit of exercice 3 code.
[TD_SR.git] / TD1 / exo3 / BufferCirc.java
CommitLineData
396bc743
JB
1\r
2\r
3/**\r
4 * implementation du producteur consommateur avec un buffer circulaire\r
5 */\r
6public class BufferCirc {\r
7\r
8 private Object[] tampon;\r
9 private int taille;\r
10 private int prem, der, nbObj;\r
11\r
12\r
13 public BufferCirc (int t) {\r
14 taille = t;\r
15 tampon = new Object[taille];\r
16 prem = 0;\r
17 der = 0;\r
18 nbObj = 0;\r
19 }\r
20\r
21\r
22 public boolean isEmpty() {\r
23 return nbObj == 0;\r
24 }\r
25\r
26\r
27 public boolean isFull() {\r
28 return nbObj == taille;\r
29 }\r
30\r
31\r
32 public synchronized void depose(Object obj) {\r
33 if (!isFull()) {\r
34 nbObj++;\r
35 tampon[prem] = obj;\r
36 prem = (prem + 1) % taille;\r
37 System.out.println(Thread.currentThread().getName() + " a depose " + (Integer)obj);\r
38 } else {\r
39 System.out.println("Buffer plein pour " + Thread.currentThread().getName());\r
40 }\r
41 }\r
42\r
43\r
44 public synchronized Object preleve() {\r
45 Object outObj = null;\r
46 if (isEmpty()) {\r
47 nbObj--;\r
48 outObj = tampon[der];\r
49 der = (der + 1) % taille;\r
50 System.out.println(Thread.currentThread().getName() + " a preleve " + (Integer)outObj);\r
51 } else {\r
52 System.out.println("Buffer vide pour " + Thread.currentThread().getName());\r
53 }\r
54 return outObj;\r
55 }\r
56\r
57} // fin class BufferCirc\r