wood burning stoves*
The moose likes Threads and Synchronization and the fly likes subclassing Thread vs. implementing Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "subclassing Thread vs. implementing Runnable" Watch "subclassing Thread vs. implementing Runnable" New topic
Author

subclassing Thread vs. implementing Runnable

Nikolay Gudovich
Greenhorn

Joined: Jan 11, 2005
Posts: 12
hi everybody!

I've had a job interview today. One of the questions was: "Why would you ever want to subclass thread rather than implement Runnable?"

Well I wasn't ready I said that subclassing Thread violated basic design principles and that in most cases one would use Runnable instead.

I couldn't think of a scenario, when subclassing Thread would be a good choice. I probably missed something. Please help me. The reason of my confusion is my lack of Threading experience.

Thanks a lot.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Search this site and Google. This is a very common question that's been discussed myriad times before.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
You are right. Check this.
Thread and this Thread
[ February 23, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Nice threads, but they don't answer the question. The second thread ends by asking exactly the same question. And nobody answered.

So, why would you ever prefer extending Thread over implementing Runnable?

Nikolay, examine Thread carefully and see if there are any useful methods a subclass might want to use or expose. Then remember any chunk of code can get access to the Thread it's running in. Are any of those useful bits protected? Or could a Runnable call its own Thread with one more step?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Nikolay Gudovich
Greenhorn

Joined: Jan 11, 2005
Posts: 12
I agree that all those posts don't answer my question. I simply want a scenario when extending Thread is a good idea.

JDocs.com
There is a small article attached to the JavaDoc.

It sates: "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."

"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."

So my understanding is that one should subclass Thread when he/she really *wants* more specialized lightweight process. Here comes the question to people with an OS background: "EXAMPLE PLEASE"

Cheers and thanks a lot for answering.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Good question - but better answered in the Threads forum. Moving...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm not a threading guru myself but this conversation brought to mind
something I read in chapter 14 of Bruce Eckel's "Thinking in Java" 2nd Edition.

It's a footnote:


"Runnable was in Java.1.0, while inner classes were not introduced until Java 1.1, which may partially account for the existence of Runnable. Also, traditional multithreading architectures focused on a function to be run rather than an object. My preference is always to inherit from Thread if I can; it seems cleaner and more flexible to me."


Most opinions that I read seem to prefer implementing Runnable.
[ February 23, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: subclassing Thread vs. implementing Runnable