• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

subclassing Thread vs. implementing Runnable

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search this site and Google. This is a very common question that's been discussed myriad times before.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. Check this.
Thread and this Thread
[ February 23, 2005: Message edited by: Srinivasa Raghavan ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Nikolay Gudovich
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question - but better answered in the Threads forum. Moving...
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic