X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TD1%2Fexo2%2FThreadJob.java;fp=TD1%2Fexo2%2FThreadJob.java;h=dae83baa750588b57e0523d1a0797e56241951e9;hb=06566a55e1640e3c2c22785dd5ce7d19c947940a;hp=0000000000000000000000000000000000000000;hpb=7e42d822cf647a90586c45a7b2f5c649f8f81cb9;p=TD_SR.git diff --git a/TD1/exo2/ThreadJob.java b/TD1/exo2/ThreadJob.java new file mode 100644 index 0000000..dae83ba --- /dev/null +++ b/TD1/exo2/ThreadJob.java @@ -0,0 +1,27 @@ +import java.util.Random; + +public class ThreadJob implements Runnable { + + ThreadJob(String name) { + setName(name); + } + + public void setName(String name) { + Thread.currentThread().setName(name); + } + + public void run() { + for (int j = 0; j < 10; j++) { + Random rand = new Random(); + int sleep_time = rand.nextInt(201); + try { + Thread.sleep(sleep_time); + } + catch(InterruptedException e) { + // this part is executed when an exception (in this example InterruptedException) occurs + } + String threadName = Thread.currentThread().getName(); + System.out.println(threadName + " has slept for " + sleep_time + " ms for the " + (j + 1) + " times"); + } + } +}