Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

invoking run() method  RSS feed

 
Lalit Kapoor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code:

public class Test extends Thread{
public Test(){
run();
}
public void run(){
try{
while(true){
System.out.println("Inside run");
Thread.sleep(1000);
}
}catch(Exception e){
System.out.println("Error");
}
}
public static void main(String args[]){
Test t = new Test();
}
}
Runs perfectly fine and displays "Inside run" continuously.The thing to note in this code is that run() method is directly invoked, instead of using the start() method. What is the difference of starting a thread by invoking run() method directly as shown in the above code as compared to using the start() method
 
Natarajan Shankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No this is one of basic dobut most of people will have. This code also work fine. But run() is not actual threading, it is normal method call in the program. The correct way is calling t.start() in main() method, at that point only you will leave to processor to work on your thread.
 
Priya Soparkar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
To add something more to above comment, when u directly call
thread.run() and u have implementation of your run method , the JVM will treat this "run" method as any other method in the class (but not like the thread's "run" method.) So even if you get the output which is printing some stuff in your run method (exactly like the thread execution), this does not use the Thread funcionality .

Whereas when the user creates a new java thread and calls thread.run() , the JVM internally allocated an Operating system thread , does some other OS level processing for you and internally calls "run" method as a callback method.

So, for executing the above functionality as a thread , u must call thread.start instead of calling run method directly.

Thanks,
Priya
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Priya Soparkar:

Whereas when the user creates a new java thread and calls thread.run() , the JVM internally allocated an Operating system thread , does some other OS level processing for you and internally calls "run" method as a callback method.



Just to make a very minor correction...you always need to call threadObject.start() and not run(). Phew! Threads are tricky arent' they?! :-) Nevertheless...you figured it out right! :-)
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add my 2 cents

When you think about thread you should think about the thread of execution and not about Thread object.

The only way to create new thread of execution in Java is to create Thread object and call start() method. Untill you call start() method JVM does not allocate new thread of execution so by calling the run() method (of the Thread object or any Runnable for that matter) you process the method on the current thread of execution.

You can simply test it by adding one line to the run() method



This will output main if you call run() directly or name of the thread if you call start().
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!