TD2: Fix a warning for object serialization.
[TD_SR.git] / TD1 / exo2 / ThreadJob.java
CommitLineData
b0f9b27c 1import java.util.concurrent.ThreadLocalRandom;
7e42d822 2
06566a55 3public 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++) {
b0f9b27c 15 int sleep_time = ThreadLocalRandom.current().nextInt(201);
7e42d822
JB
16 try {
17 Thread.sleep(sleep_time);
18 }
19 catch(InterruptedException e) {
20 // this part is executed when an exception (in this example InterruptedException) occurs
ce28a021 21 System.err.println("InterruptedException: " + e);
7e42d822
JB
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}