T3.java
import java.io.*;
/**
Print clown names from three threads.
Removed the call to setDaemon(); replaced with interrupt() calls
to stop child threads.
*/
public class T3 extends Thread
{
public static void main(String[] argv)
{
Thread one = new T3_thread("Bozo",200);
Thread two = new T3_thread("Homey",300);
Thread three = new T3_thread("Happy",400);
System.out.println("Press ENTER when you're satisfied...");
one.start();
two.start();
three.start();
try {
System.in.read();
System.out.println("ENTER pressed.");
one.interrupt();
two.interrupt();
three.interrupt();
}
catch(IOException e) {
System.out.println("Exception: " + e);
}
System.out.println("Ending main()...");
}
}
class T3_thread extends Thread
{
public T3_thread(String name,int delay)
{
this.name = name;
this.delay = delay;
// setDaemon(true);
}
public void run()
{
try {
while (true) {
System.out.println(name);
sleep(delay);
}
}
catch(InterruptedException e) {
System.out.println(name + " interrupted: " + e);
}
}
private String name;
private int delay;
}