class MainClass extends Thread {
static String message[] = { "A", "B", "C,", "D,", "E","F." };
public static void main(String args[]) {
MainClass thread1 = new MainClass("thread1: ");
MainClass thread2 = new MainClass("thread2: ");
thread1.start();
thread2.start();
boolean thread1IsAlive = true;
boolean thread2IsAlive = true;
do {
if (thread1IsAlive && !thread1.isAlive()) {
thread1IsAlive = false;
System.out.println("Thread 1 is dead.");
}
if (thread2IsAlive && !thread2.isAlive()) {
thread2IsAlive = false;
System.out.println("Thread 2 is dead.");
}
} while (thread1IsAlive || thread2IsAlive);
}
public MainClass(String id) {
super(id);
}
public void run() {
String name = getName();
for (int i = 0; i < message.length; ++i) {
try {
sleep((long) (3000 * Math.random()));
} catch (InterruptedException x) {
System.out.println("Interrupted!");
}
System.out.println(name + message[i]);
}
}
}
thread2: A
thread1: A
thread2: B
thread1: B
thread1: C,
thread1: D,
thread2: C,
thread2: D,
thread1: E
thread2: E
thread2: F.
Thread 2 is dead.
thread1: F.
Thread 1 is dead.