• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between start() and run() method?

 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • 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()?
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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();
}
}
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
swapnl patil
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • 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: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
"GREEN THREAD" ? I never heard about the this. I Know the daemon Thread.
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Minal, That is correct. I am glad I could be of help.
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Ashwin Aditya
Greenhorn
Posts: 2
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic