TD2: Add several producers and consumers to exo3.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 7 Mar 2018 19:56:11 +0000 (20:56 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 7 Mar 2018 19:56:11 +0000 (20:56 +0100)
Also avoid to print twice the same information.

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TD1/exo3/BufferCirc.java
TD1/exo3/Main.java

index 335420aa60e278d80fb5ef6aa64078a7264fff1c..1c13e54baffc0acb926b717c5d66f5bec8fddc37 100644 (file)
@@ -39,8 +39,8 @@ public class BufferCirc {
                nbObj++;\r
                tampon[prem] = obj;\r
                prem = (prem + 1) % taille;\r
+               //System.out.println(Thread.currentThread().getName() + " a depose " + (Integer)obj);\r
                notify();\r
-               System.out.println(Thread.currentThread().getName() + " a depose " + (Integer)obj);\r
        }\r
 \r
 \r
@@ -55,8 +55,8 @@ public class BufferCirc {
                nbObj--;\r
                outObj = tampon[der];\r
                der = (der + 1) % taille;\r
+               //System.out.println(Thread.currentThread().getName() + " a preleve " + (Integer)outObj);\r
                notify();\r
-               System.out.println(Thread.currentThread().getName() + " a preleve " + (Integer)outObj);\r
                return outObj;\r
        }\r
 \r
index 929534c3ed627fa6676fea5a6170ab8d6d30f60c..7f0cd0eb8568ce0cdcb2f56a7f23a9d80a2462b4 100644 (file)
@@ -6,15 +6,24 @@ public class Main {
 \r
 \r
        public static void main (String[] args) {\r
-               BufferCirc b = new BufferCirc(20);\r
+               final int BUFFER_SIZE = 20;\r
+               final int PROD_NUMBER = 10;\r
+               final int CONS_NUMBER = 10;\r
+               BufferCirc b = new BufferCirc(BUFFER_SIZE);\r
                Producteur p = new Producteur(b);\r
                Consommateur c = new Consommateur(b);\r
-               Thread P1 = new Thread(p);\r
-               P1.setName("P1");\r
-               Thread C1 = new Thread(c);\r
-               C1.setName("C1");\r
-               P1.start();\r
-               C1.start();\r
+               Thread[] P = new Thread[PROD_NUMBER];\r
+               Thread[] C = new Thread[CONS_NUMBER];\r
+               for (int i = 0; i < P.length; i++) {\r
+                       P[i] = new Thread(p);\r
+                       P[i].setName("P" + i);\r
+                       P[i].start();\r
+               }\r
+               for (int i = 0; i < C.length; i++) {\r
+                       C[i] = new Thread(c);\r
+                       C[i].setName("C" + i);\r
+                       C[i].start();\r
+               }\r
        }\r
 \r
 }\r