File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Should we call 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" Watch "Should we call" New topic

Should we call

Jimmy Thomas

Joined: Aug 17, 2005
Posts: 19
Is it a good practice to call 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..

Creating & starting new Thread
Creating & starting new Thread, with MyRun Sleeping
Woke up
Creating & starting new MyThread
Creating & starting new MyThread, with MyRun Sleeping
Woke up
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 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:
subject: Should we call
It's not a secret anymore!