Thread Class run() Method in Java : Free Java Tutorials and Online Training

Thread Class public void run() method is the only method of the Runnable interface and the classes which aim to execute their code in a separate thread of execution for the job assigned to that thread. First, implement the Runnable interface. Subsequently, define this method and put all the code expected to be executed in the separate thread inside the scope of this run() method.

Start() method creates a new thread which is responsible for the execution of run() method. If we directly call thread’s run() method, it will not create a new thread and it will execute run() method just like a normal method call by the main thread.

Starting Thread without defining run() method

Thread class run() method has an empty implementation. If we don’t override run() method then Thread class’ run() method gets called and output is empty. Now, if we are not overriding run() method, it means that we are not defining any job for that thread and in that case what is the need of thread then? It is highly recommended to override the run() method and that is how multithreading concept is used in Java.

// We create our own class named MyThread that extends Thread class
class MyThread extends Thread
{	
	
}

// Main class
public class Demo
{
	public static void main(String args[])
	{
		// Creating two thread objects of our class named MultiThread and starting both the threads concurrently.
		MyThread thread = new MyThread();
		thread.start();
		
		// Main Thread executing 
		System.out.println("Main Thread is running");
				
	}
}

As we run this program, it will print the only as we have not defined run() method in MyThread class. Here, Thread class’ run() method gets called and as it has an empty implementation, that thread does nothing.

Output:

Main Thread is running

Overloading Thread class run() method

Overloading run() method is possible but thread’s start() method will always call no arguments run() method. If we want to call overloaded run() method, which has arguments, then we have to explicitly call it like a normal method call.

// We create our own class named MyThread that extends Thread class
class MyThread extends Thread
{	
	// No-Argument run method
	public void run()
	{
		System.out.println("No-Arguments Child Thread is running");		
	}
	
	// Overloaded run method having arguments
	public void run(int number)
	{
		System.out.println("Child Thread wants to print the number " + number);
	}

}

// Main class
public class Demo
{
	public static void main(String args[])
	{
		// Creating a thread object of our class named MyThread and starting the thread.
		MyThread thread = new MyThread();
		thread.start();	
	}
}

Thread Class will ignore the overloaded run() method unless we explicitly call it ourselves from the main method. The Thread class expects a run() method with no-arguments and it will execute that in a separate call stack after the thread has been started.

Output:

No-Arguments Child Thread is running

If we want to execute the overloaded run() method, then we should call it directly from the main method. The main thread executes the overloaded run() method and not the newly created thread.

// We create our own class named MyThread that extends Thread class
class MyThread extends Thread
{	
	// No-Argument run method
	public void run()
	{
		System.out.println("No-Arguments Child Thread is running");		
	}
	
	// Overloaded run method having arguments
	public void run(int number)
	{
		System.out.println("Child Thread wants to print the number " + number);
	}

}

// Main class
public class Demo
{
	public static void main(String args[])
	{
		// Creating a thread object of our class named MyThread and starting the thread.
		MyThread thread = new MyThread();
		thread.start();
		thread.run(5);
	}
}

Output:

Child Thread wants to print the number 5
No-Arguments Child Thread is running

 

Leave a Comment

Your email address will not be published. Required fields are marked *