run() Method in Java Thread

16 Nov, 2021
The run() method is available in the thread class constructed using a separate Runnable object. Otherwise, this method does nothing and returns. We can call the run() method multiple times. 
The run() method can be called in two ways which are as follows:
Using the start() method.Using the run() method itself.
Using the start() method.
Using the run() method itself.
Syntax:
public void run()  
{    
    //statements  
}  
We can use the start() method by using a thread object.
 Step 1: Create run method.
 Step 2: Create an object for the class.
Syntax: Usingstart obj=new Usingstart(); 
 Step 3: Create a thread object by passing the class variable.
Syntax: Thread t1 =new Thread(obj);    
 Step 4: This will call the run() method.
Syntax: t1.start();
Example:
Java
// Java program to demonstrate the working // of run() method using the start() method  public class Usingstart implements Runnable  {        //run method    public void run()      {            System.out.println("This thread is running");        }              //main method    public static void main(String args[])      {         //create the object        Usingstart obj=new Usingstart();                   //create thread object by passing the class variable        Thread t1 =new Thread(obj);                      // this will call run() method           t1.start();        }    }
This thread is running
We can use the run() method by using a thread object.
Step 1: Create run method.
Step 2: Create an object for the class.
Syntax: Usingstart obj=new Usingstart();     
Step 3: This will call the run() method.
Syntax: obj.run();
Example:
Java
// Java program to demonstrate // the working of run() method  public class Usingstart implements Runnable  {        //run method    public void run()      {            System.out.println("This thread is running");        }              //main method    public static void main(String args[])      {         //create the object        Usingstart obj=new Usingstart();                      // this will call run() method           obj.run();        }    }
This thread is running
In Java, we can also call the run() method multiple times. By using a for loop, we can call the same run method numerous times.
Example:
Java
// Java program to call the// run() method multiple times  public class RumMultipleTimes extends Thread {    public void run()    {        // run 10 times each        for (int i = 1; i <= 10; i++) {            System.out.println("Running " + i + " Time");        }    }        public static void main(String args[])    {        // create object        RumMultipleTimes t1 = new RumMultipleTimes();          // call the run method        t1.run();        t1.run();    }}
Running 1 Time
Running 2 Time
Running 3 Time
Running 4 Time
Running 5 Time
Running 6 Time
Running 7 Time
Running 8 Time
Running 9 Time
Running 10 Time
Running 1 Time
Running 2 Time
Running 3 Time
Running 4 Time
Running 5 Time
Running 6 Time
Running 7 Time
Running 8 Time
Running 9 Time
Running 10 Time
We can also overload the run()method. By changing the parameters, we can get exact things from the methods.
Example: Java program that takes two overloaded methods with one parameter and another with no parameters.
Here we first call the parameter method and then call the no parameter method.
Java
// Java Program to illustrate the behavior of// run() method overloading  class GFG extends Thread {      // run method with no parameters    public void run()    {        System.out.println("no parameters method");    }      // run method with  parameters    public void run(int i)    {        System.out.println("single parameter method");    }}  public class Test {    public static void main(String[] args)    {        // create an object        GFG t = new GFG();          // call the parameter method        t.run(8);          // call the no parameter method        t.run();    }}
single parameter method
no parameters method
Java-Multithreading
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
List Interface in Java with Examples
Strings in Java
Different ways of Reading a text file in Java
Reverse an array in Java
How to remove an element from ArrayList in Java?
Checked vs Unchecked Exceptions in Java
ArrayList get(index) Method in Java with Examples
ArrayList to Array Conversion in Java : toArray() Methods
Comparator Interface in Java with Examples
Exceptions in Java