TD1: Use thread optimized random number generator.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 7 Mar 2018 19:11:02 +0000 (20:11 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 7 Mar 2018 19:11:02 +0000 (20:11 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TD1/exo3/Consommateur.java
TD1/exo3/Producteur.java

index 5a548813f2add051d965d812369aba355b257d04..7aa8f3004d90a4ebc5fbc62e0c12f8be4d22d7ac 100644 (file)
@@ -1,3 +1,4 @@
+import java.util.concurrent.ThreadLocalRandom;\r
 \r
 \r
 public class Consommateur implements Runnable {\r
 \r
 \r
 public class Consommateur implements Runnable {\r
@@ -16,7 +17,7 @@ public class Consommateur implements Runnable {
                        val = (Integer)buffer.preleve();\r
                        System.out.println (Thread.currentThread().getName() + " a preleve " + val);\r
                        try {\r
                        val = (Integer)buffer.preleve();\r
                        System.out.println (Thread.currentThread().getName() + " a preleve " + val);\r
                        try {\r
-                               Thread.sleep((int)(Math.random()*1000));\r
+                               Thread.sleep(ThreadLocalRandom.current().nextInt(1001));\r
                        }\r
                        catch (InterruptedException e) {}\r
                }\r
                        }\r
                        catch (InterruptedException e) {}\r
                }\r
index 10eab6d55ec99bc5e25278842a1b1a5e30a6a8a6..d836af2a754f29d0d25d76e0413cf456cbe0e99b 100644 (file)
@@ -1,3 +1,4 @@
+import java.util.concurrent.ThreadLocalRandom;\r
 \r
 \r
 public class Producteur implements Runnable {\r
 \r
 \r
 public class Producteur implements Runnable {\r
@@ -17,7 +18,7 @@ public class Producteur implements Runnable {
                        System.out.println (Thread.currentThread().getName() +  " a depose " + val);\r
                        val++;\r
                        try {\r
                        System.out.println (Thread.currentThread().getName() +  " a depose " + val);\r
                        val++;\r
                        try {\r
-                               Thread.sleep((int)(Math.random()*100));\r
+                               Thread.sleep(ThreadLocalRandom.current().nextInt(101));\r
                        }\r
                        catch (InterruptedException e) {}\r
                }\r
                        }\r
                        catch (InterruptedException e) {}\r
                }\r