Java provides Thread class to manage different activities performed by a thread with the help of different methods defined under Thread class.

How to create thread in Java

There are two ways to create thread in java. They are:

  1. by extending Thread class
  2. by implementing Runnable interface
Thread class

Thread class provided different constructors and methods to perform different activities of a thread to increase the overall efficiency of the program. Thread class extends Object class and implements Runnable interface

Some important methods of Thread class in java
  • public void run( ) : it is used to perform the action for a thread. It means what we expect from a thread to do is written in this method only i.e the business logic.
  • public void start( ) : it is used to start the execution of a thread where JVM calls the run ( ) on the specified thread.
  • public void sleep (long milliseconds) : it sleeps the current executing thread for a specified number of milliseconds.
  • public int getPriority ( ) : it is used to get the priority of any specified thread.
  • public int setPriority (int priority ) : it is used to change the priority of a thread.
  • public String getName( ) : this method of Thread class is used to return the name of the specified thread.
  • public void setName(String name ): it allow to set name of the specified thread
  • public Thread getcurrentThread( ) : returns the reference of the currently executing thread.

1. Creating thread by extending Thread class

  • First we have to declare the class by extending it by Thread class so that all the methods of the Thread class can be easily accessed.
  • Secondly, override the public void run( ) method of Thread class which is responsible for executing the sequence of code that the thread will execute.
  • Finally, create an instance of Thread class by creating object of the user-defined class and call the start ( ) method to initiate the thread execution.
class Sample extends Thread
 {
public void run()
  {
 System.out.println("Child Thread");
   }
}

The business logic will go in public void run() method because if we want to perform any task from the thread then we have to override public void run() method of the Thread class and to start the execution of the thread we have to first create instance of the Thread class and then call the start() method which will invoke no argument run() method.

Example 1:

class Sample extends Thread
 {
  public void run()
   {
   System.out.println("I am a running thread");
    }
 
 public static void main(String args[])
  {
 Sample t1=new Sample();
 t1.start();
   }
 }

Output: I am a running thread

Explanation:
  • The statement “Sample t1=new Sample()” instantiates a new object of class Sample which means thread “t1” is created. The thread is not yet running. The thread “t1” is in a new state.
  • When start() method is called on instance of a thread (t1) then it causes the thread to move into the runnable state.
  • After that, JVM schedules the thread to run by invoking run() method and the thread is said to be in running state.

Example II.

class Sample extends Thread
 {
  public void run() // business logic here,what thread will perform
   {
   System.out.println("I am a running thread");
    }
 }
 
class Test
  {
 public static void main(String args[])
  {
 Sample t1=new Sample();
 Sample t2=new Sample();
 t1.start(); // first thread is started
 t2.start(); //second thread is started
   }
 }

Output:

I am a running thread
I am a running thread

2. Creating thread by implementing Runnable interface

Sometimes it is very difficult to extend Thread class for importance of the program then we prefer to implement Runnable interface. The Runnable interface declares the run ( ) method that is required for implementing threads to our programs.

  • Declare the user-defined class by implementing Runnable interface.
  • Implement and override the run ( ) method.
  • Create an object of the user-defined class.
  • Create an object of the Thread class and pass the object of the user-defined class as an argument to the Thread class.
  • Now, call the start ( ) method to run the thread.
Example I :
class Sample implements Runnable
 {
  public void run()
   {
   System.out.println("I am a thread invoked by Runnable interface");
    }
 
 public static void main(String args[])
  {
 Sample obj=new Sample();
 Thread  t1=new Thread(obj);
 t1.start();
   }
 }

Output: I am a thread invoked by Runnable interface

Explanation:
  • Firstly, in the above program in the given statement “Sample obj=new Sample()” instance (obj) of user-define class (Sample) is passed as an argument to the Thread class so that it can be executed by the Thread class.
  • Secondly, a new thread (“t1”) is created and it invokes the start() method which internally calls the run ( ) method of the implementer class.
Example II.
class Sample implements Runnable
 {
  public void run()
   {
   System.out.println("Thread running.....");
    }
 
 public static void main(String args[])
  {
 Sample obj1=new Sample();
 Sample obj2=new Sample();
 Thread  t1=new Thread(obj1);
 Thread  t2=new Thread(obj2);
 t1.start();
 t2.start();
   }
 }

Output:

 Thread running.....
 Thread running.....