run() Method in Java Thread
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.
Usingstart obj=new Usingstart();
Step 3: Create a thread object by passing the class variable.
Thread t1 =new Thread(obj);
Step 4: This will call the run() method.
t1.start();
Example:
// Java program to demonstrate the working
// of run() method using the start() method
class Usingstart implements Runnable
{
// Overriding the run method
@Override
public void run() {
System.out.println("This thread is running");
}
}
public class Geeks
{
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();
}
}
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 for creating object
Usingstart obj=new Usingstart();
Step 3: This will call the run() method. Syntax for running the thread.
obj.run();
Example:
// Java program to demonstrate
// the working of run() method
class Usingstart implements Runnable
{
// Override the run method
@Override
public void run() {
System.out.println("This thread is running");
}
}
public class Geeks
{
public static void main(String args[])
{
// Create the object
Usingstart obj=new Usingstart();
// this will call run() method
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 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();
}
}
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 Program to illustrate the behavior of
// run() method overloading
class TestGeeks 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 Geeks
{
public static void main(String[] args)
{
// Create an object
TestGeeks t = new TestGeeks();
// Call the parameter method
t.run(8);
// Call the no parameter method
t.run();
}
}
Output
single parameter method no parameters method