From 8084e5ebb29e8b6d14109f8b06ef6f01db931609 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 7 Mar 2018 20:56:11 +0100 Subject: [PATCH] TD2: Add several producers and consumers to exo3. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Also avoid to print twice the same information. Signed-off-by: Jérôme Benoit --- TD1/exo3/BufferCirc.java | 4 ++-- TD1/exo3/Main.java | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) 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(); + } } } -- 2.34.1