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()?
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.
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
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.
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.
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
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.
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.
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
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