import java.util.concurrent.*;
class MyRunnable implements Runnable {
public void run() {
try {
int randomNum = ThreadLocalRandom.current().nextInt(0, 200);
Thread.sleep(randomNum);
System.out.printf("Slept for %d seconds\n", randomNum);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
}
}
}
class Main {
public static void main(String[] args) {
Thread[] n = new Thread[10];
for (int i = 0; i < 10; i++) {
n[i] = new Thread(new MyRunnable());
n[i].start();
}
for (int i = 0; i < 10; i++) {
try {
n[i].join();
} catch (InterruptedException e) {
System.out.println("Interrupted!");
}
}
System.out.println("All threads done!");
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: