beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Why don’t we call run() method directly, why call start() method?

By Chaitanya Singh | Filed Under: Multithreading

We can call run() method if we want but then it would behave just like a normal method and we would not be able to take the advantage of multithreading. When the run method gets called though start() method then a new separate thread is being allocated to the execution of run method, so if more than one thread calls start() method that means their run method is being executed by separate threads (these threads run simultaneously).

On the other hand if the run() method of these threads are being called directly then the execution of all of them is being handled by the same current thread and no multithreading will take place, hence the output would reflect the sequential execution of threads in the specified order. Did it confuse you? Lets have a look at the below code to understand this situation.

Calling run() method

public class RunMethodExample implements Runnable{
   public void run(){  
      for(int i=1;i<=3;i++){  
	try{
              Thread.sleep(1000);
	   }catch(InterruptedException ie){
		ie.printStackTrace();
	    }  
	 System.out.println(i);  
      }  
   }  
   public static void main(String args[]){  
      Thread th1 = new Thread(new RunMethodExample(), "th1");
      Thread th2 = new Thread(new RunMethodExample(), "th2"); 
      th1.run();  
      th2.run(); 
   }
}

Output:

1
2
3
1
2
3

As you can observe in the output that multithreading didn’t place here, it because both the run methods are being handled by the current thread. that treated them like normal methods and had them executed in the specified order rather then having them executed simultaneously. Thread scheduler didn’t play any role here.

Calling start() method

Multithreading takes place and the output reflects simultaneous execution of threads.

public class RunMethodExample2 {
   public void run(){  
      for(int i=1;i<=3;i++){  
	 try{
               Thread.sleep(1000);
	 }
         catch(InterruptedException ie){
	       ie.printStackTrace();
         }  
         System.out.println(i);  
      }  
   }  
   public static void main(String args[]){  
      Thread th1 = new Thread(new RunMethodExample(), "th1");
      Thread th2 = new Thread(new RunMethodExample(), "th2"); 
      th1.start();  
      th2.start(); 
   }
}

Output:

1
1
2
2
3
3

As you can see the output reflects the simultaneous execution of threads.

Enjoyed this post? Try these related posts

  1. Thread life cycle in java and thread scheduling
  2. Multithreading in java with examples
  3. Can we start a Thread twice in Java?
  4. Basics: All about Java threads
  5. What is the difference between a process and a thread in Java?
  6. Thread join() method in Java with example

Comments

  1. Albert Nguyen says

    June 6, 2016 at 4:42 AM

    Hello ,

    Could you please explain to me:
    “When the run method gets called though start() method then a new separate thread is being allocated to the execution of run method”

    Thank you for your explanation indeed,
    Albert

    Reply

Leave a Reply Cancel reply

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

Multithreading

  • Multithreading
  • Process vs Thread
  • Thread Life Cycle
  • Daemon thread
  • thread join()
  • Why call start()
  • Thread starting twice?
  • Multithreading Interview Q

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap