X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TD1%2Fexo2%2FThreadJob.java;h=754f54ebdc4a1e704b94d8cde2a7d4bca715a97e;hb=ce28a0211974450cf5df46e2065fb39350e029ec;hp=dae83baa750588b57e0523d1a0797e56241951e9;hpb=06566a55e1640e3c2c22785dd5ce7d19c947940a;p=TD_SR.git diff --git a/TD1/exo2/ThreadJob.java b/TD1/exo2/ThreadJob.java index dae83ba..754f54e 100644 --- a/TD1/exo2/ThreadJob.java +++ b/TD1/exo2/ThreadJob.java @@ -1,4 +1,4 @@ -import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; public class ThreadJob implements Runnable { @@ -12,13 +12,13 @@ public class ThreadJob implements Runnable { public void run() { for (int j = 0; j < 10; j++) { - Random rand = new Random(); - int sleep_time = rand.nextInt(201); + int sleep_time = ThreadLocalRandom.current().nextInt(201); try { Thread.sleep(sleep_time); } catch(InterruptedException e) { // this part is executed when an exception (in this example InterruptedException) occurs + System.err.println("InterruptedException: " + e); } String threadName = Thread.currentThread().getName(); System.out.println(threadName + " has slept for " + sleep_time + " ms for the " + (j + 1) + " times");