Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Thread.start() and Thread.run()?

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.

like image 306
Saurabh Gokhale Avatar asked Apr 20 '10 10:04

Saurabh Gokhale


3 Answers

No, you can't. Calling run will execute run() method in the same thread, without starting new thread.

like image 174
Marko Avatar answered Oct 06 '22 01:10

Marko


Why do we call the start() method, which in turn calls the run() 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.

like image 26
Chez Avatar answered Oct 05 '22 23:10

Chez


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.

like image 26
aditya Avatar answered Oct 05 '22 23:10

aditya