Difference betwixt Thread.start() together with Thread.run() method inwards Java?

If you lot remember, a Thread is started inward Java past times calling the start() method of java.lang.Thread class, but if you lot acquire to a greater extent than you lot 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 enquiry comes, why can't you lot only telephone phone the run() method instead of calling the start() method? Since start() is calling the run() anyway, calling run conduct should accept the same resultant every bit calling the start, no?

Well, this is 1 of the tricky multi-threading questions you lot volition uncovering on Java interviews together with it's non slowly every bit it looks, peculiarly if you lot accept one-half cognition virtually threads inward Java.

The clitoris a fast 1 on hither is that when you lot conduct telephone phone the run() method together with thence the code within run() method is executed inward the same thread which calls the run method. JVM will non practise a novel thread until you lot telephone phone the start method.

On the other hand, when you lot telephone phone the Thread.start() method, together with thence the code within run() method volition live on executed on a novel thread, which is genuinely created past times the start() method (see Java Fundamentals: The Java Language).

Another key divergence betwixt start together with run method to scream upwards is that you lot tin forcefulness out telephone phone the run method multiple time, JVM volition non throw whatsoever fault but when you lot cannot telephone phone the start() method on same thread instance.

The starting fourth dimension time, t.start() volition practise a novel thread but the instant fourth dimension it volition throw java.lang.IllegalStateException, because the thread is already started together with you lot cannot restart it again, you lot tin forcefulness out exclusively interruption a thread inward Java. Once it died it's gone.



Difference betwixt start() together with run() method of Thread class

The run() method comes from the Runnable interface but the start() method is exclusively declared inward the Thread class. Since java.lang.Thread shape implements Runnable interface, you lot tin forcefulness out access the run() method from an instance of Thread class.

I accept created next Java programme to demonstrate you lot the difference of calling start() method on the thread together with calling the run() method directly from the principal thread.

Remember, inward both the cases the run() method volition live on called but when you lot telephone phone the start() method together with thence it volition live on called past times a novel thread.

On the other hand, if you lot telephone phone the run() method conduct than it volition live on called on the same thread i.e. principal thread inward our case.

Don't believe? run the code together with 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 shape inward Java. Just scream upwards that when you lot conduct telephone phone the run()  * method, code written within the run() method volition live on executed inward the calling  * thread, but when you lot telephone phone the start() method together with thence a novel thread volition live on  * 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 select handgrip of block             e.printStackTrace();         }                  System.out.println( Thread.currentThread().getName() + " Calling the run() method of Thread");         t.run();     } }

together with hither is the output:

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


You tin forcefulness out clearly run into that when our principal thread (the thread which executes the main() method inward Java) calls the start() method than a novel thread amongst mention Thread-0 is created together with run() method is executed past times that thread, but if you lot conduct telephone phone the run() method than its executed on the same principal thread inward Java.

 a Thread is started inward Java past times calling the  Difference betwixt Thread.start() together with Thread.run() method inward Java?


This is 1 of the fundamentals of threading inward Java, which is oftentimes acquire overlooked past times Java developers unless you lot accept read a mass like Java Threads By Scott Oaks, which explains every key multi-threading concept together with thread basics inward expert detail.

 a Thread is started inward Java past times calling the  Difference betwixt Thread.start() together with Thread.run() method inward Java?

If you lot similar courses improve than books together with thence you lot tin forcefulness out besides banking corporation jibe out Java Fundamentals Part 1,2, a gratis course of didactics from Pluralsight to acquire to a greater extent than virtually basics of threads inward Java.


That's all virtually the difference betwixt start() together with run() method inward Java. Just scream upwards that fifty-fifty though start() method internally calls the run() method, its principal utilization is to practise a novel thread. If you lot conduct telephone phone the run() method than a novel thread volition non live on created instead run() volition acquire executed on the same thread. It agency you lot 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() together with sleep() method inward Java? (answer)
  2. 5 differences betwixt wait() together with 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 interruption a thread inward Java? (answer)
  6. Difference betwixt wait() together with yield() method inward Java? (answer)
  7. Difference betwixt CyclicBarrier together with CountDownLatch inward Java? (answer)
  8. Difference betwixt notify() together with notifyAll() inward Java? (answer)

Further Learning
Java Fundamentals Part 1,2
Java Concurrency inward Practice
Applying Concurrency together with Multi-threading to Common Java Patterns

Thanks for reading this article, if you lot similar this tutorial together with thence delight portion amongst your friends together with colleagues. If you lot accept whatsoever questions or feedback together with thence delight drib a comment. 

Subscribe to receive free email updates:

0 Response to "Difference betwixt Thread.start() together with Thread.run() method inwards Java?"

Posting Komentar