Reputation: 46395
Why do we call the start()
method, which in turn calls the run()
method?
Can't we directly make a call to run()
?
Please give an example where there is a difference.
Upvotes: 46
Views: 23342
Reputation: 31375
No, you can't. Calling run will execute run()
method in the same thread, without starting new thread.
Upvotes: 54
Reputation: 509
Why do we call the
start()
method, which in turn calls therun()
method?
No that's imprecise. start()
in turn does not call the run method.
instead it starts the thread which executes the run method. This is native.
Can't we directly make a call to
run()
?
If you call run()
directly you don't start the thread, you just execute the method on the same caller method.
Please give an example where there is a difference.
There are millions on the web. Hence I don't duplicate.
Upvotes: 26
Reputation: 21
this is the work done by start method
synchronized public void start()
{
//it calls start0() method internally and start0() method does below
//create a real child thread and register with thread scheduler
//create runtime stack for child thread
//call run() on underlying Runtime object
}
Upvotes: -1
Reputation: 3798
Because start();
is synchronized and run();
is simple/regular method. Same as java knows starting execution from main();
method. As thread knows starting execution from run();
Here is the Source code from Thread
Class:
run();
code:
@Override
public void run() { // overriding from Runnable
if (target != null) {
target.run();
}
}
start();
code:
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* 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.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
In short start();
is the manager of threads, how to manage etc. and run();
is tarting point of thread's working.
Upvotes: 0
Reputation: 664
If you call run() directly, the code gets executed in the calling thread. By calling start(), a new thread is created other than the main thread and is executed in parallel.
Upvotes: 0
Reputation: 25
Main difference is that when program calls start() method a new Thread is created and code inside run() method is executed in new Thread.If you call run() method directly no new Thread is created and code inside run() will execute on current Thread.
Most of the time calling run() is bug or programming mistake because caller has intention of calling start() to create new thread and this error can be detect by many static code coverage tools like findbugs. If you want to perform time consuming task than always call start() method otherwise your main thread will stuck while performing time consuming task if you call run() method directly. Another difference between start vs run in Java thread is that you can not call start() method twice on thread object. once started, second call of start() will throw IllegalStateException in Java while you can call run() method twice.
Upvotes: 0
Reputation: 495
Actually thread.start()
creates a new thread and have its own execution scenario.
but thread.run()
not creating any new thread, instead it execute the run method in the current running thread.
So guys if you are using thread.run()
then think that what is the use of multi-threading if you want only one thread execute all run method.
Upvotes: 4
Reputation: 21480
you can't run directly the run() method. Whenever start your thread by using thread.start(), then the run() method has been called and performed the further operation.
Upvotes: 0
Reputation: 19225
Because start() doesnt just call run(). It starts a new thread and in that thread calls run().
Upvotes: 2