what is the difference between start() and run() method , if no runnable interface is used , run method is overridden inside the thread.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   meenu_gupta
Posted On:   Monday, April 28, 2003 12:01 AM

I have a simple for loop and a progressbar.Inside for loop progreaa bar value is changed the string is painted again.
This method is called from inside the run method of thread. This run method is overiden inside the thread like
Thread t = new Thread (){
public void run(){

}
}
t.start();

if i am giving t.start() , progress bar is painted but if i am giving t.run(), even though it is entering inside the run method and calling other method , but proces is not being done?
what is difference between start() and run() method.

Re: what is the difference between start() and run() method , if no runnable interface is used , run method is overridden inside the thread.

Posted By:   Laurent_Mihalkovic  
Posted On:   Sunday, June 8, 2003 09:34 AM

Hi Meenu,



A simple way to understand what happens is to see the Thread class as the engine for executing code. As such, you can imagine that before your code can be ran separately from the main execution thread, lots has to be done internally by the JVM. So the logical conclusion is that you would call one method that would tell the engine to make the necessary preparations, and that in turn the engine would let you know that all is done and allow you to execute your code. Once you can visualize the JVM having to do some preparation work, it is natural that there would be two different methods. When you call run() yourself, your code does indeed execute, however the JVM does not have a chance to create the separate thread for your code to execute separately.


cheers

laurent

Re: what is the difference between start() and run() method , if no runnable interface is used , run method is overridden inside the thread.

Posted By:   Anonymous  
Posted On:   Monday, April 28, 2003 03:26 AM

Hi Meenu,


The purpose of the start() method is to let the JVM know that the thread is ready to run. So, when we call t.start(), JVM automatically calls the run() method of the thread t. In threading, run() method of the thread should not be called directly.


what happens if we call run() directly?


In this case, the run method would be called just like any ordinary method NOT as the run() method in a Thread class. There wont be any compile time errors, but it wont be executed like a thread.


Hope this helps..........
About | Sitemap | Contact