From: Jérôme Benoit Date: Fri, 9 Mar 2018 08:36:45 +0000 (+0100) Subject: TD1: Make some constructors. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d3ae3c42030dcfc0d1de38673f221c7618863ca9;p=TD_SR.git TD1: Make some constructors. Signed-off-by: Jérôme Benoit --- 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) {