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
Joined: Jan 11, 2005
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"
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
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 ]