This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference b/w extending Thread and implementing an interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference b/w extending Thread and implementing an interface" Watch "Difference b/w extending Thread and implementing an interface" New topic

Difference b/w extending Thread and implementing an interface

Sathish Kumar Govindan

Joined: Jan 23, 2007
Posts: 18

Hi Ranchers,
Please tell me the difference b/w extending Thread and implementing an interface.By using Both the ways we can implement the thread concept. so, i want to Know at what circumstance i have to use them....
If i am extending the thread, then i can't extend one more class. Apart from this reason is there anything......

Thanks in Advance,,
G.sathish Kumar.

Certified on SCJP 5.0, OCPJWCD, Spring3.2, OCEJWSD
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116

I do not beieve there is any difference between the two apart you are only allowed to extend one class so if you extend thread you would not be able to extend anything else.

This is why implement runnable is probably the best option.

Quote found:

"If you're considering having your class extend Thread ask yourself, "Have I invented a new kind of thread, or do I simply have something that executes in a thread?". If it's the latter (and it usually is) then you should strongly consider implementing Runnable instead.

There are a few reasons for this. The first is mainly aestheic. The extends & implements keywords denote an "is a" relationship. Extending Thread declares that your class is a new kind of light-weight process. On the other hand, implementing Runnable declares that your class is a task that can execute in a thread. In other words, it's not the process itself, but rather what the process is doing. The distinction is subtle but it can be important.

There are practical reasons too. For one thing, extending Thread means that you cannot extend anything else. If you MUST extend from another class then you'll have to implement Runnable instead.

Also, several methods in the Thread class are synchronized. If you declare synchronized methods in your Thread subclass then you run the risk that they will interfere with the normal thread functions. You might even encounter a deadlock. It's safest to avoid synchronizing on the same object for two different purposes.

A good rule of thumb is to always implement Runnable and extend Thread only if you have a very good reason. If you think about it, I think you'll find very few reasons not to implement Runnable."

Hope this helps,

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
Also see this FAQ entry: ExtendingThreadVsImplementingRunnable
Have you checked out Aspose?
subject: Difference b/w extending Thread and implementing an interface
jQuery in Action, 3rd edition