class Main {
public static void main(String[] args) {
- final int THREADS_NUMBER = 3;
- ExThread threadArray[] = new ExThread[THREADS_NUMBER];
+ final int THREADS_NUMBER = 3;;
Thread t[] = new Thread[THREADS_NUMBER];
- for (int i = 0; i < threadArray.length; i++) {
- threadArray[i] = new ExThread(String.valueOf(i));
- t[i] = new Thread(threadArray[i]);
+ for (int i = 0; i < t.length; i++) {
+ ThreadJob threadJob = new ThreadJob(String.valueOf(i));
+ t[i] = new Thread(threadJob);
t[i].start();
}
- for (int i = 0; i < threadArray.length; i++) {
+ for (int i = 0; i < t.length; i++) {
try {
t[i].join();
}
catch (InterruptedException e) {
- // this part is executed when an exception (in this example InterruptedException) occurs
+ // this part is executed when an exception (in this example InterruptedException) occurs
}
}
System.out.println("Threads execution finished");
import java.util.Random;
-public class ExThread implements Runnable {
- String threadName;
+public class ThreadJob implements Runnable {
- ExThread(String name) {
+ ThreadJob(String name) {
setName(name);
}
// this part is executed when an exception (in this example InterruptedException) occurs
}
String threadName = Thread.currentThread().getName();
- System.out.println(threadName + " has sleept for " + sleep_time + " ms for the " + (j + 1) + " times");
+ System.out.println(threadName + " has slept for " + sleep_time + " ms for the " + (j + 1) + " times");
}
}
}