Sunday, November 24, 2019

Difference Betwixt Thread.Start() As Well As Thread.Run() Method Inward Java?

If y'all remember, a Thread is started inward Java past times calling the start() method of java.lang.Thread class, but if y'all larn to a greater extent than y'all volition uncovering out that start() method internally calls the run() method of Runnable interface to execute the code specified inward the run() method inward a split thread. Now the query comes, why can't y'all but telephone telephone the run() method instead of calling the start() method? Since start() is calling the run() anyway, calling run lead should bring the same lawsuit equally calling the start, no?

Well, this is ane of the tricky multi-threading questions y'all volition uncovering on Java interviews in addition to it's non tardily equally it looks, peculiarly if y'all bring one-half cognition nearly threads inward Java.

The play a trick on hither is that when y'all lead telephone telephone the run() method in addition to thence the code within run() method is executed inward the same thread which calls the run method. JVM will non do a novel thread until y'all telephone telephone the start method.

On the other hand, when y'all telephone telephone the Thread.start() method, in addition to thence the code within run() method volition move executed on a novel thread, which is genuinely created past times the start() method (see Java MasterClass).

Another key divergence betwixt start in addition to run method to retrieve is that y'all tin telephone telephone the run method multiple time, JVM volition non throw whatever mistake but when y'all cannot telephone telephone the start() method on same thread instance.

The start time, t.start() volition do a novel thread but the mo fourth dimension it volition throw java.lang.IllegalStateException, because the thread is already started in addition to y'all cannot restart it again, y'all tin alone time out a thread inward Java. Once it died it's gone.



Difference betwixt start() in addition to run() method of Thread class

The run() method comes from the Runnable interface but the start() method is alone declared inward the Thread class. Since java.lang.Thread cast implements Runnable interface, y'all tin access the run() method from an instance of Thread class.

I bring created next Java plan to demonstrate y'all the difference of calling start() method on the thread in addition to calling the run() method directly from the primary thread.

Remember, inward both the cases the run() method volition move called but when y'all telephone telephone the start() method in addition to thence it volition move called past times a novel thread.

On the other hand, if y'all telephone telephone the run() method lead than it volition move called on the same thread i.e. primary thread inward our case.

Don't believe? run the code in addition to run into past times yourself.  Let's run into the illustration now.

/**  * Java Program to demonstrate the divergence betwixt run() vs start() method of  * Thread cast inward Java. Just retrieve that when y'all lead telephone telephone the run()  * method, code written within the run() method volition move executed inward the calling  * thread, but when y'all telephone telephone the start() method in addition to thence a novel thread volition move  * created to execute the code written within run() method inward Java.  *   * @author WINDOWS 8  *  */ public class HelloWorldApp {      public static void main(String args[]) {         Thread t = new Thread() {              @Override             public void run() {                 System.out.println(Thread.currentThread().getName()                         + " is executed the run() method");             }         };          System.out.println( Thread.currentThread().getName() + " Calling the start() method of Thread");         t.start();                  // let's hold off until the thread completes execution         try {             t.join();         } catch (InterruptedException e) {             // TODO Auto-generated grab block             e.printStackTrace();         }                  System.out.println( Thread.currentThread().getName() + " Calling the run() method of Thread");         t.run();     } }

in addition to hither is the output:

main Calling the start() method of Thread Thread-0 is executed the run() method primary Calling the run() method of Thread primary is executed the run() method


You tin clearly run into that when our primary thread (the thread which executes the main() method inward Java) calls the start() method than a novel thread amongst refer Thread-0 is created in addition to run() method is executed past times that thread, but if y'all lead telephone telephone the run() method than its executed on the same primary thread inward Java.

 a Thread is started inward Java past times calling the  Difference betwixt Thread.start() in addition to Thread.run() method inward Java?


This is ane of the fundamentals of threading inward Java, which is ofttimes acquire overlooked past times Java developers unless y'all bring read a mass like Java Threads By Scott Oaks, which explains every key multi-threading concept in addition to thread basics inward expert detail.

 a Thread is started inward Java past times calling the  Difference betwixt Thread.start() in addition to Thread.run() method inward Java?

If y'all similar courses amend than books in addition to thence y'all tin too banking firm gibe out Java MasterClass, a class from Udemy to larn to a greater extent than nearly basics of threads inward Java.


That's all nearly the difference betwixt start() in addition to run() method inward Java. Just retrieve that fifty-fifty though start() method internally calls the run() method, its primary role is to do a novel thread. If y'all lead telephone telephone the run() method than a novel thread volition non move created instead run() volition acquire executed on the same thread. It agency y'all should ever start the thread past times calling Thread.start() method inward Java.

Related Java multi-threading Interview Questions from Java
  1. What is the divergence betwixt the yield() in addition to sleep() method inward Java? (answer)
  2. 5 differences betwixt wait() in addition to sleep() method inward Java? (answer)
  3. How to bring together multiple threads inward Java? (answer)
  4. How to halt a thread inward Java? (answer)
  5. How to time out a thread inward Java? (answer)
  6. Difference betwixt wait() in addition to yield() method inward Java? (answer)
  7. Difference betwixt CyclicBarrier in addition to CountDownLatch inward Java? (answer)
  8. Difference betwixt notify() in addition to notifyAll() inward Java? (answer)

Further Learning
Multithreading in addition to Parallel Computing inward Java
Applying Concurrency in addition to Multi-threading to Common Java Patterns
Java Concurrency inward Practice - The Book
Java Concurrency inward Practice Bundle past times Heinz Kabutz

Thanks for reading this article, if y'all similar this tutorial in addition to thence delight percentage amongst your friends in addition to colleagues. If y'all bring whatever questions or feedback in addition to thence delight drib a comment. 

No comments:

Post a Comment