T4.java
import java.io.*;
/**
Print clown names from three threads.
Use the Runnable interface rather than a Thread subclass.
*/
public class T4
{
public static void main(String[] argv)
{
Thread one = new Thread(new Run4("Bozo",400));
Thread two = new Thread(new Run4("Homey",500));
Thread three = new Thread(new Run4("Happy",600));
// one.setDaemon(true);
// two.setDaemon(true);
// three.setDaemon(true);
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 Run4 implements Runnable
{
public Run4(String name,int delay)
{
this.name = name;
this.delay = delay;
}
public void run()
{
try {
while (true) {
System.out.println(name);
Thread.sleep(delay); // We're not a subclass of Thread!
}
}
catch(InterruptedException e) {
System.out.println(name + " interrupted: " + e);
}
}
private String name;
private int delay;
}