Creating Thread: Deriving a Subclass of Thread : Create Thread « Thread « Java Tutorial






import java.io.IOException;

class TryThread extends Thread {
  public TryThread(String firstName, String secondName, long delay) {
    this.firstName = firstName;
    this.secondName = secondName;
    aWhile = delay;
    setDaemon(true);
  }
  public void run() {
    try {
      while (true) {
        System.out.print(firstName);
        sleep(aWhile);
        System.out.print(secondName + "\n");
      }
    } catch (InterruptedException e) {
      System.out.println(firstName + secondName + e);
    }
  }
  private String firstName;
  private String secondName;
  private long aWhile;
}
public class MainClass {
  public static void main(String[] args) {
    Thread first = new TryThread("A ", "a  ", 200L);
    Thread second = new TryThread("B ", "b ", 300L);
    Thread third = new TryThread("C ", "c ", 500L);
    System.out.println("Press Enter when you have had enough...\n");
    first.start();
    second.start();
    third.start();
    try {
      System.in.read();
      System.out.println("Enter pressed...\n");
    } catch (IOException e) {
      System.out.println(e);
    }
    return;
  }
}








10.1.Create Thread
10.1.1.Creating a Thread
10.1.2.Creating Thread: Deriving a Subclass of Thread
10.1.3.Creating Thread Objects: Implementing the run() Method in Runnable interface
10.1.4.Create a second thread.
10.1.5.Create a second thread by extending Thread
10.1.6.Create multiple threads.