Thread Creation

Here, you will learn how to construct a simple thread in java.

Thread Creation


In Java, an object of the Thread class can represent a thread. Thread can be implemented through any one of two ways:

  • Extending the java.lang.Thread Class
  • Implementing the java.lang.Runnable Interface

I. Extending the java.lang.Thread Class

For creating a thread a class have to extend the Thread Class. For creating a thread by this procedure you have to follow these steps:

  1. Extend the java.lang.Thread Class.
  2. Override the run( ) method in the subclass from the Thread class to define the code executed by the thread.
  3. Create an instance of this subclass. This subclass may call a Thread class constructor by subclass constructor.
  4. Invoke the start( ) method on the instance of the class to make the thread eligible for running.

The following program demonstrates a single thread creation extending  the "Thread" Class:

class MyThread extends Thread{

  String s=null;

  MyThread(String s1){
  public void run(){
public class RunThread{
  public static void main(String args[]){
  MyThread m1=new MyThread("Thread started....");


  Output of the Program is :


C:\j2se6\thread>java RunThread
Thread started....

Download this example

II. Implementing the java.lang.Runnable Interface

The procedure for creating threads by implementing the Runnable Interface is as follows:

  1. A Class implements the Runnable Interface, override the run() method  to define the code executed by thread. An object of this class is Runnable Object.
  2. Create an object of Thread Class by passing a Runnable object as argument.
  3. Invoke the start( ) method on the instance of the Thread class.

The following program demonstrates the thread creation implenting the Runnable interface:

class MyThread1 implements Runnable{
  Thread t;
String s=null;

  MyThread1(String s1){

  t=new Thread(this);
  public void run(){
public class RunableThread{
  public static void main(String args[]){
  MyThread1 m1=new MyThread1("Thread started....");

However, this program returns the output same as of the output generated through the previous program.

Output of the Program is:


C:\j2se6\thread>java RunableThread
Thread started....

Download this example

There are two reasons for implementing a Runnable interface preferable to extending the Thread Class:

  1. If you extend the Thread Class, that means that subclass cannot extend any other Class, but if you implement Runnable interface then you can do this.
  2. The class implementing the Runnable interface can avoid the full overhead of Thread class which can be excessive.

  join() & isAlive() methods:


The following program demonstrates the join() & isAlive() methods:

class DemoAlive extends Thread {
  int value;

  public DemoAlive(String str){

 public void run(){  
  while (value < 5){  
  System.out.println(getName() ": " (value++));
  catch (Exception e) {}
  System.out.println("Exit from thread: " + getName());

public class DemoJoin{  

public static void main(String[] args){
  DemoAlive da = new DemoAlive("Thread a");
  DemoAlive db = new DemoAlive("Thread b");
  System.out.println("Wait for the child threads to finish.");

  if (!da.isAlive())
  System.out.println("Thread A not alive.");


  if (!db.isAlive())
  System.out.println("Thread B not alive.");
  catch (Exception e) { }
  System.out.println("Exit from Main Thread.");



Output of this program is:



C:\j2se6\thread>java DemoJoin
Wait for the child threads to finish.
Thread a: 0
Thread b: 0
Thread a: 1
Thread b: 1
Thread a: 2
Thread b: 2
Thread a: 3
Thread b: 3
Thread a: 4
Thread b: 4
Exit from thread: Thread a
Thread A not alive.
Exit from thread: Thread b
Thread B not alive.
Exit from Main Thread.



Download this example

Share on Google+Share on Google+

Thread Creation

Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Thread Creation   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
July 29, 2011

how to run these program in eclipse.?????? we canot give class name has DemoAlive extends Thread.????what should be class name????
March 10, 2012
variable in thread

in the first program why we using the variable s which is string and why is null
geeta rani
July 17, 2012

These program help me to understand the java
August 17, 2012

not in a clear manner