Java Threads

Extending Thread

public class SimpleThread extends Thread {
  public SimpleThread(String str) {
    super(str); //sets the name of this thread to str.
  }
  public void run() {
    for (int i = 0; i < 10; i++) {
      //print i and the name of this thread.
      System.out.println(i + " " + getName());
      try {
        sleep((long)(Math.random() * 1000));
      } catch (InterruptedException e) {}
    }
    System.out.println("DONE! " + getName());
  }
}

public class TwoThreadsDemo {
  public static void main (String[] args) {
    new SimpleThread("Jamaica").start();
    new SimpleThread("Fiji").start();
  }
}

José M. Vidal .

3 of 8