• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Difference between start() and run() method?

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can execute thread by calling start() method as well by calling run() methos.
What is difference between executing thread using start() method and run() method()?
 
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just as a main(String[] argument_list) is an entry point for a desktop java application , so similarly run() is an entry point for a object being executed as a Thread.

start() enable the Thread to be controlled in states. Explicity invoking run() , violates the flow and state of a Thread execution.
start() is not invoked for the main method thread or "system" or group threads created/set up by the VM. Any new functionality added to this method in the future may have to also be added to the VM.
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your unsure just always use start never call run directly ..

When you call Start , run gets invoked in a new thread (multi threading) , call run directly and it will get invoked on your calling thread (single threading) you probably never want to do that and it confuses the reader (if you did want that effect but the implementation in a new method and have the run call it, you can then call your new method if you want it run single threaded).

Thread.run just calls your run method ie 1.6 src is

public void run() {
if (target != null) {
target.run();
}
}
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

minal silimkar wrote:We can execute thread by calling start() method as well by calling run() methos.



This statement is a bit misleading. The start() method starts the Thread and executes the run() method's code in the new Thread. Calling the run() method does not start a new Thread, it executes the run() method's code in the current (calling) Thread. So the difference is: Using the start() method you have a new OS level Thread and the Thread's code is executed concurrently to the current Thread's code, but using the run() method, there is no new OS level Thread so it's code gets executed immediately and in sequence with the current Thread.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are different states of Thread.



when we call the start method it push the thread in to Ready state & after that thread is on mercy of thread scheduler.
when thread get chance to run in running state then JVM call the Run method.

I hope this will clarify your all doubt.
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swapnl patil wrote:There are different states of Thread.



when we call the start method it push the thread in to Ready state & after that thread is on mercy of thread scheduler.
when thread get chance to run in running state then JVM call the Run method.

I hope this will clarify your all doubt.



Is it a "GREEN THREAD" you are talking about? Generally is it JVM / OS that call it
 
swapnl patil
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"GREEN THREAD" ? I never heard about the this. I Know the daemon Thread.
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swapnl patil wrote:"GREEN THREAD" ? I never heard about the this. I Know the daemon Thread.



Have a look at http://en.wikipedia.org/wiki/Green_threads
 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per Steve's explaination


The start() method starts the Thread and executes the run() method's code in the new Thread.


What I have understood from your statement is, in given code below, at line no. 9 we are just creating object, not creating thread and line no. 10 will create thread.
If I comment line no. 10 and uncomment line no. 11. it will not create thread, simply it will execute run method of same class.

Thank you, Steve
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Minal, That is correct. I am glad I could be of help.
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sidharth Pallai wrote:If you just invoke run() directly, it's executed on the calling thread, just like any other method call. A new call stack never gets created.Thread.start() is required to actually create a new thread/new call stack so that the runnable's run method is executed in parallel.start() enable the Thread to be controlled in states.
start() is not invoked for the main method thread or "system" or group threads created/set up by the VM. Any new functionality added to this method in the future may have to also be added to the VM.

 
Greenhorn
Posts: 2
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
start method when called will create a new thread on the jvm, whereas calling the run method will be like a normal method call i.e. no new thread will be created.
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic