run() Method in Java Thread
Last Updated :
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.
Syntax:
public void run()
{
//statements
}
1. Using the start() method
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
public class Usingstart implements Runnable
{
public void run()
{
System.out.println( "This thread is running" );
}
public static void main(String args[])
{
Usingstart obj= new Usingstart();
Thread t1 = new Thread(obj);
t1.start();
}
}
|
Output
This thread is running
2. Using the run() method
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
public class Usingstart implements Runnable
{
public void run()
{
System.out.println( "This thread is running" );
}
public static void main(String args[])
{
Usingstart obj= new Usingstart();
obj.run();
}
}
|
Output
This thread is running
Calling run() Method Multiple Times
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
public class RumMultipleTimes extends Thread {
public void run()
{
for ( int i = 1 ; i <= 10 ; i++) {
System.out.println( "Running " + i + " Time" );
}
}
public static void main(String args[])
{
RumMultipleTimes t1 = new RumMultipleTimes();
t1.run();
t1.run();
}
}
|
Output
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
Overloading the run() Method
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
class GFG extends Thread {
public void run()
{
System.out.println( "no parameters method" );
}
public void run( int i)
{
System.out.println( "single parameter method" );
}
}
public class Test {
public static void main(String[] args)
{
GFG t = new GFG();
t.run( 8 );
t.run();
}
}
|
Output
single parameter method
no parameters method
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...