start() and run() method

Before moving further, you should know about the Memory representation of thread creation in java.
Thread execution starts when the JVM calls the start() method. In this post we will read why we call the start() method instead of run() method. What is the difference start() and run() method.

Here is the table content of the article will we will cover this topic.
1. How does the start() method work?
2. If we run() method called instead of start() method

As we know we have two ways to create a thread in java. In both ways, we need to provide the body to run() method. Actually the run() method is the body of thread that executes when JVM calls the start() method. But the question arises why we can start() method instead of run() method?

To find the answer we will look how does the start() method and run() method work?

How does the start() method work?

As we know whenever a thread executes the JVM creates a new stack and stores the thread data into the stack. If you are not familiar with How to thread creation in memory you should read it first. Whenever the JVM invokes the start() method there are some certain operations that are performed.

1. After invoking the start() method, JVM will create a run-time stack for the particular thread. All
the method calls of the particular thread enter in the stack and know as a stack frame.
2. The method body of thread will be executed.
3. Value is returned and the current stack frame is popped from the call stack.

NOTE: The start() method creates a separate call stack for each thread. and then run() is called by JVM. The run() popped the current stack frame from the call stack. 

If we run() method called instead of start() method

If we call the run() method instead of start() method then the program executes as a normal program and it is not considered as a separate thread. The run() method doesn’t create a separate stack for the new thread. The run() method goes onto the current call stack rather than at the beginning of a new call stack. So, this will work as a normal program because each thread needs a separate call.

start() and run() method

Let’s see how does program work if we call start() method:

public class StartAndRunMethod 
{
	public static void main(String arg[])
	{
		ThreadExample thread1 = new ThreadExample();
		thread1.setName("Thread1");
		thread1.start();
		
		ThreadExample thread2 = new ThreadExample();
		thread2.setName("Thread2");
		thread2.start();
	}
}

class ThreadExample extends Thread
{
  public void run()
  {
    for(int i = 1; i <= 5 ; i++)
    {
      System.out.println(Thread.currentThread().getName()+"is running and value of i = "+i);
      try 
      {
	Thread.sleep(1000);
      } 
      catch (InterruptedException e) 
      {
	e.printStackTrace();
      }
    }
  }
}

Output:
Thread1 is running and value of i = 1
Thread2 is running and value of i = 1
Thread1 is running and value of i = 2
Thread2 is running and value of i = 2
Thread2 is running and value of i = 3
Thread1 is running and value of i = 3
Thread1 is running and value of i = 4
Thread2 is running and value of i = 4
Thread1 is running and value of i = 5
Thread2 is running and value of i = 5

In the above example start() method create separate call stack for each thread. So, both are running and printing values.

Now we will see how does program work if we call run() method:

public class StartAndRunMethod 
{
	public static void main(String arg[])
	{
		ThreadExample thread1 = new ThreadExample();
		thread1.setName("Thread1");
		thread1.run();
		
		ThreadExample thread2 = new ThreadExample();
		thread2.setName("Thread2");
		thread2.run();
	}
}

class ThreadExample extends Thread
{
  public void run()
  {
    for(int i = 1; i <= 5 ; i++)
    {
     System.out.println(Thread.currentThread().getName()+"is running and value of i = " +i);
      try 
      {
	 Thread.sleep(1000);
      } 
      catch (InterruptedException e) 
      {
	 e.printStackTrace();
      }
    }
  }
}
main is running and value of i = 1
main is running and value of i = 2
main is running and value of i = 3
main is running and value of i = 4
main is running and value of i = 5
main is running and value of i = 1
main is running and value of i = 2
main is running and value of i = 3
main is running and value of i = 4
main is running and value of i = 5

In the above example run() method doesn’t create a separate call stack for each thread. So, It’s working as a normal program.

Leave a Comment