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