1 import java
.util
.Random
;
3 public class ExThread
implements Runnable
{
6 ExThread(String name
) {
10 public void setName(String name
) {
11 Thread
.currentThread().setName(name
);
15 for (int j
= 0; j
< 10; j
++) {
16 Random rand
= new Random();
17 int sleep_time
= rand
.nextInt(201);
19 Thread
.sleep(sleep_time
);
21 catch(InterruptedException e
) {
22 // this part is executed when an exception (in this example InterruptedException) occurs
24 String threadName
= Thread
.currentThread().getName();
25 System
.out
.println(threadName
+ " has sleept for " + sleep_time
+ " ms for the " + (j
+ 1) + " times");