Commit | Line | Data |
---|---|---|
7e42d822 JB |
1 | import java.util.Random; |
2 | ||
3 | public class ExThread implements Runnable { | |
4 | String threadName; | |
5 | ||
6 | ExThread(String name) { | |
7 | setName(name); | |
8 | } | |
9 | ||
10 | public void setName(String name) { | |
11 | Thread.currentThread().setName(name); | |
12 | } | |
13 | ||
14 | public void run() { | |
15 | for (int j = 0; j < 10; j++) { | |
16 | Random rand = new Random(); | |
17 | int sleep_time = rand.nextInt(201); | |
18 | try { | |
19 | Thread.sleep(sleep_time); | |
20 | } | |
21 | catch(InterruptedException e) { | |
22 | // this part is executed when an exception (in this example InterruptedException) occurs | |
23 | } | |
24 | String threadName = Thread.currentThread().getName(); | |
25 | System.out.println(threadName + " has sleept for " + sleep_time + " ms for the " + (j + 1) + " times"); | |
26 | } | |
27 | } | |
28 | } |