Create and Start thread start() Example Java

Starting Thread start() is very easier in Java. It is the occasion to prove that Java is simple to practice. Just extend Thread to your class and create an object of your class. The object is nothing but a thread of your class. Start reading.

In Java, a thread can be created in two ways.

a) by extending Thread class
b) by implementing Runnable interface

The following example gives how to create thread and start the thread. Let us learn through an example thread start().
public class Demo extends Thread
{
  public void run()           
  {
    System.out.println("From run() method");
  }      
  public static void main(String args[])
  {
    Demo d1 = new Demo();
    d1.start();
  }
}

thread start()

Demo d1 = new Demo();
d1.start();

In the above code, you say d1 is an object of Demo class. You are right; but at the same time, d1 is a thread of Demo class. It is so because Demo class extends Thread class. Observe, it is so simple to create a thread in Demo class. Just extend the class and create an object. The object becomes thread automatically.

The thread d1 is created but it is inactive. An inactive thread is not eligible for microprocessor time. To make the thread active, call start() method on the thread as d1.start(). start() method is defined in Thread class.

The start() method implicitly calls run() method. run() method is a concrete (non-abstract) method of Thread class. That is, calling start() method calls automatically run() method. This is the job of JVM. run() is the heart of the thread and any code you would like to be executed by the thread, write in the run() method.

Do not think of calling d1.run() directly. Thread looses lot of activities implicitly done by JVM.

You may be interested to know Difference between thread start() and run()

4 thoughts on “Create and Start thread start() Example Java”

  1. i didn’t got last line…..:-

    Do not think of calling d1.run() directly. Thread looses lot of activities implicitly done by JVM.

    and d1.run() directly is working then?

    1. When a thread is started, its life cycle is maintained by OS. Developer informs the OS, he is starting a thread by calling start() method on the thread. start() in turn calls run() method internally. But if you call run() directly, OS does not maintain the life cycle. Thread methods may work, but the life cycle is not maintained like thread garbage collection etc.

  2. import java.util.*;
    class fg
    {
    static String array=”aeiou”;
    public static void mt(String k)
    {
    int count=0;
    for(int i=0;i<k.length();i++)
    {
    for(int j=0;j<array.length();j++)
    {
    if((k.charAt(i))==(array.charAt(j)))
    {
    count++;
    }
    }
    }
    System.out.println(count);
    }
    }
    class replace
    {
    public static void main(String ar[])
    {
    fg d=new fg();
    Scanner s=new Scanner(System.in);
    String b=s.next();
    d.mt(b);
    }
    }
    Sir when i enter a string without any space program gives the correct output whereas if i enter a string with space (for example:my name is)it doesn't show the output

Leave a Comment

Your email address will not be published.