From: Jérôme Benoit Date: Wed, 7 Mar 2018 19:56:11 +0000 (+0100) Subject: TD2: Add several producers and consumers to exo3. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=8084e5ebb29e8b6d14109f8b06ef6f01db931609;p=TD_SR.git TD2: Add several producers and consumers to exo3. Also avoid to print twice the same information. Signed-off-by: Jérôme Benoit --- diff --git a/TD1/exo3/BufferCirc.java b/TD1/exo3/BufferCirc.java index 335420a..1c13e54 100644 --- a/TD1/exo3/BufferCirc.java +++ b/TD1/exo3/BufferCirc.java @@ -39,8 +39,8 @@ public class BufferCirc { nbObj++; tampon[prem] = obj; prem = (prem + 1) % taille; + //System.out.println(Thread.currentThread().getName() + " a depose " + (Integer)obj); notify(); - System.out.println(Thread.currentThread().getName() + " a depose " + (Integer)obj); } @@ -55,8 +55,8 @@ public class BufferCirc { nbObj--; outObj = tampon[der]; der = (der + 1) % taille; + //System.out.println(Thread.currentThread().getName() + " a preleve " + (Integer)outObj); notify(); - System.out.println(Thread.currentThread().getName() + " a preleve " + (Integer)outObj); return outObj; } diff --git a/TD1/exo3/Main.java b/TD1/exo3/Main.java index 929534c..7f0cd0e 100644 --- a/TD1/exo3/Main.java +++ b/TD1/exo3/Main.java @@ -6,15 +6,24 @@ public class Main { public static void main (String[] args) { - BufferCirc b = new BufferCirc(20); + final int BUFFER_SIZE = 20; + final int PROD_NUMBER = 10; + final int CONS_NUMBER = 10; + BufferCirc b = new BufferCirc(BUFFER_SIZE); Producteur p = new Producteur(b); Consommateur c = new Consommateur(b); - Thread P1 = new Thread(p); - P1.setName("P1"); - Thread C1 = new Thread(c); - C1.setName("C1"); - P1.start(); - C1.start(); + Thread[] P = new Thread[PROD_NUMBER]; + Thread[] C = new Thread[CONS_NUMBER]; + for (int i = 0; i < P.length; i++) { + P[i] = new Thread(p); + P[i].setName("P" + i); + P[i].start(); + } + for (int i = 0; i < C.length; i++) { + C[i] = new Thread(c); + C[i].setName("C" + i); + C[i].start(); + } } }