aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Should we call super.run()... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Should we call super.run()..." Watch "Should we call super.run()..." New topic
Author

Should we call super.run()...

Jimmy Thomas
Greenhorn

Joined: Aug 17, 2005
Posts: 19
Is it a good practice to call super.run() when we override the run() method of a Thread sub-class. The behaviour that I've noticed is that if we create a new subclassed Thread object using a Runnable object, then the run method will also get invoked for the Runnable object along with the Thread's run method sequentially.

This is how the code in java.lang.Thread class works, it calls the target's, i.e. the Runnable's run method.



The below code exhibits this scenario..


Output:
----------------------------------------------
Creating & starting new Thread
Creating & starting new Thread, with MyRun
MyRun.run()... Sleeping
Woke up
Creating & starting new MyThread
MyThread.run()
123
Creating & starting new MyThread, with MyRun
MyRun.run()... Sleeping
Woke up
MyThread.run()
123
----------------------------------------------
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I think you are creating a threadful monster here

When using threads we have the choice of two simple possibilities:
1. Extend the class Thread and override the run method (not calling super.run()). Then start an instance of the new thread class with start() method.

2. Implement interface Runnable. Pass an instance of the implemented Runnable to the constructor of a Thread object. Then start the new Thread object with the start() method.

That's it.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Should we call super.run()...