1 import java
.util
.concurrent
.ThreadLocalRandom
;
3 public class ThreadJob
implements Runnable
{
5 ThreadJob(String name
) {
9 public void setName(String name
) {
10 Thread
.currentThread().setName(name
);
14 for (int j
= 0; j
< 10; j
++) {
15 int sleep_time
= ThreadLocalRandom
.current().nextInt(201);
17 Thread
.sleep(sleep_time
);
19 catch(InterruptedException e
) {
20 // this part is executed when an exception (in this example InterruptedException) occurs
21 System
.err
.println("InterruptedException: " + e
);
23 String threadName
= Thread
.currentThread().getName();
24 System
.out
.println(threadName
+ " has slept for " + sleep_time
+ " ms for the " + (j
+ 1) + " times");