TD1: Make some constructors.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 9 Mar 2018 08:36:45 +0000 (09:36 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 9 Mar 2018 08:36:45 +0000 (09:36 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TD1/exo3/Consommateur.java
TD1/exo3/Main.java
TD1/exo3/Producteur.java

index 7aa8f3004d90a4ebc5fbc62e0c12f8be4d22d7ac..28a5efa78427e74ebc6f5aab516fc4f4a3b115fd 100644 (file)
@@ -5,11 +5,18 @@ public class Consommateur implements Runnable {
 \r
        private BufferCirc buffer;\r
 \r
-\r
        public Consommateur(BufferCirc b) {\r
                buffer = b;\r
        }\r
 \r
+       public Consommateur(BufferCirc b, String name) {\r
+               buffer = b;\r
+               setThName(name);\r
+       }\r
+\r
+       public void setThName(String name) {\r
+               Thread.currentThread().setName(name);\r
+       }\r
 \r
        public void run() {\r
                Integer val;\r
index 28592461759aae7bbcfecec22aa8c6eb298ab0af..1d65a13a9c7c79c173936f60fbcf59e0a65b2723 100644 (file)
@@ -10,17 +10,15 @@ public class Main {
                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[] 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] = new Thread(new Producteur(b));\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] = new Thread(new Consommateur(b));\r
                        C[i].setName("C" + i);\r
                        C[i].start();\r
                }\r
index d836af2a754f29d0d25d76e0413cf456cbe0e99b..f7f3c474b57e49d830ec32e6aeed80fff028d915 100644 (file)
@@ -11,6 +11,14 @@ public class Producteur implements Runnable {
                buffer = b;\r
        }\r
 \r
+       public Producteur(BufferCirc b, String name) {\r
+               buffer = b;\r
+               setThName(name);\r
+       }\r
+\r
+       public void setThName(String name) {\r
+               Thread.currentThread().setName(name);\r
+       }\r
 \r
        public void run() {\r
                while (true) {\r