aspose file tools*
The moose likes Threads and Synchronization and the fly likes Difference between start() and run() method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Difference between start() and run() method?" Watch "Difference between start() and run() method?" New topic
Author

Difference between start() and run() method?

minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
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()?


Minal Silimkar
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

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.


Thanks & Regards
Sidharth Pallai
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 420
    
    2

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();
}
}


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.

Steve
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
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

Joined: Apr 21, 2008
Posts: 134

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

Joined: Aug 13, 2007
Posts: 80
"GREEN THREAD" ? I never heard about the this. I Know the daemon Thread.
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

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
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

Hi Minal, That is correct. I am glad I could be of help.
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

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

Joined: Jun 06, 2012
Posts: 2

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.


when there's life in dreams, there is a dream in life
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between start() and run() method?