Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would you call java's thread.run() instead of thread.start()?

When would you call Java's thread.run() instead of thread.start()?

like image 556
blank Avatar asked Nov 04 '08 18:11

blank


People also ask

Can we call the Run method instead of start in thread?

No, you can not directly call run method to start a thread. You need to call start method to create a new thread. If you call run method directly , it won't create a new thread and it will be in same stack as main.

What happens if we call the run () method instead of start () method?

The run method is just another method. If you call it directly, then it will execute not in another thread, but in the current thread. If start isn't called, then the Thread created will never run. The main thread will finish and the Thread will be garbage collected.

What is difference between calling start () and run () method of thread?

start method of thread class is implemented as when it is called a new Thread is created and code inside run() method is executed in that new Thread. While if run method is executed directly than no new Thread is created and code inside run() will execute on current Thread and no multi-threading will take place.

Can we use run () instead of start ()?

If run() method is called directly instead of start() method in Java code, run() method will be treated as a normal overridden method of the thread class (or runnable interface). This run method will be executed with in the context of the current thread not in a new thread.


1 Answers

You might want to call run() in a particular unit test that is concerned strictly with functionality and not with concurrency.

like image 109
Paul Croarkin Avatar answered Oct 13 '22 03:10

Paul Croarkin