From d3ae3c42030dcfc0d1de38673f221c7618863ca9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 9 Mar 2018 09:36:45 +0100 Subject: [PATCH] TD1: Make some constructors. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TD1/exo3/Consommateur.java | 9 ++++++++- TD1/exo3/Main.java | 6 ++---- TD1/exo3/Producteur.java | 8 ++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/TD1/exo3/Consommateur.java b/TD1/exo3/Consommateur.java index 7aa8f30..28a5efa 100644 --- a/TD1/exo3/Consommateur.java +++ b/TD1/exo3/Consommateur.java @@ -5,11 +5,18 @@ public class Consommateur implements Runnable { private BufferCirc buffer; - public Consommateur(BufferCirc b) { buffer = b; } + public Consommateur(BufferCirc b, String name) { + buffer = b; + setThName(name); + } + + public void setThName(String name) { + Thread.currentThread().setName(name); + } public void run() { Integer val; diff --git a/TD1/exo3/Main.java b/TD1/exo3/Main.java index 2859246..1d65a13 100644 --- a/TD1/exo3/Main.java +++ b/TD1/exo3/Main.java @@ -10,17 +10,15 @@ public class Main { 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[] 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] = new Thread(new Producteur(b)); P[i].setName("P" + i); P[i].start(); } for (int i = 0; i < C.length; i++) { - C[i] = new Thread(c); + C[i] = new Thread(new Consommateur(b)); C[i].setName("C" + i); C[i].start(); } diff --git a/TD1/exo3/Producteur.java b/TD1/exo3/Producteur.java index d836af2..f7f3c47 100644 --- a/TD1/exo3/Producteur.java +++ b/TD1/exo3/Producteur.java @@ -11,6 +11,14 @@ public class Producteur implements Runnable { buffer = b; } + public Producteur(BufferCirc b, String name) { + buffer = b; + setThName(name); + } + + public void setThName(String name) { + Thread.currentThread().setName(name); + } public void run() { while (true) { -- 2.34.1