-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
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");