Thread Creation

In this chapter you will learn:

  1. What are the two ways to create a thread
  2. Implementing Runnable interface
  3. Extending Thread class

Two ways to create a thread

Java defines two ways in which this can be accomplished:

  • You can implement the Runnable interface.
  • You can extend the Thread class, itself.

Implementing Runnable

To implement Runnable, a class need only implement a single method called run(), which is declared like this:

void run();

Inside run(), you will define the code that constitutes the new thread. run() establishes the entry point for another, concurrent thread of execution within your program. This thread will end when run() returns.

After you create a class that implements Runnable, you will instantiate an object of type Thread from within that class. Thread defines several constructors.

After the new thread is created, it will not start running until you call its start() method. start() executes a call to run().

class NewThread implements Runnable {
  Thread t;/*from j  av a  2  s  .c  om*/

  NewThread() {
    t = new Thread(this, "Demo Thread");
    System.out.println("Child thread: " + t);
    t.start(); // Start the thread
  }
  public void run() {
    try {
      for (int i = 5; i > 0; i--) {
        System.out.println("Child Thread: " + i);
        Thread.sleep(500);
      }
    } catch (InterruptedException e) {
      System.out.println("Child interrupted.");
    }
    System.out.println("Exiting child thread.");
  }
}

public class Main {
  public static void main(String args[]) {
    new NewThread(); // create a new thread
    try {
      for (int i = 5; i > 0; i--) {
        System.out.println("Main Thread: " + i);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println("Main thread interrupted.");
    }
    System.out.println("Main thread exiting.");
  }
}

Extending Thread

The extending class must override the run() method, which is the entry point for the new thread. It must also call start() to begin execution of the new thread.

Here is the preceding program rewritten to extend Thread:

class NewThread extends Thread {
  NewThread() {//  j a v a  2 s.com
    super("Demo Thread");
    System.out.println("Child thread: " + this);
    start(); // Start the thread
  }
  public void run() {
    try {
      for (int i = 5; i > 0; i--) {
        System.out.println("Child Thread: " + i);
        Thread.sleep(500);
      }
    } catch (InterruptedException e) {
      System.out.println("Child interrupted.");
    }
    System.out.println("Exiting child thread.");
  }
}

public class Main {
  public static void main(String args[]) {
    new NewThread(); // create a new thread
    try {
      for (int i = 5; i > 0; i--) {
        System.out.println("Main Thread: " + i);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println("Main thread interrupted.");
    }
    System.out.println("Main thread exiting.");
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to use isAlive() and join()
  2. How to join the background thread
Home » Java Tutorial » Thread
Thread introduction
Thread Name
Thread Main
Thread sleep
Thread Creation
Thread join and is alive
Thread priorities
Thread Synchronization
Interthread Communication
Thread Step
Thread suspend, resume, and stop
ThreadGroup
BlockingQueue
Semaphore
ReentrantLock
Executor
ScheduledThreadPoolExecutor