File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes extending thread vs implementing runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "extending thread vs implementing runnable" Watch "extending thread vs implementing runnable" New topic

extending thread vs implementing runnable

arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
could sb describe me the precise difference between extending the thread class and implementing runnable.
This doubt arose while reading the Java 2 certification by Simon Roberts,phillip heller, michael ernest, chapter 7 Thread Fundamental.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
When you want to create a new thread of execution (a separate process in a sense) you need to create a runnable object whose run method will be executed. You can do this in one of two ways as you mentioned:
1. extend the Thread class
2. implement the Runnable interface and use that as the target of a thread.
When you extend Thread an object of your class has all of the behavior built into it that it needs to run - basically it inherits the run method (from Runnable that Thread implements) and the start method from Thread. All you need to do is override the run method and then start it and your off. This is good if you dont need to subclass any other class except Thread.
On the other hand, if you have a hierarchy of classes and you need to extend another class but still need your new class to be multi-thread capable then you can implement the Runnable interface. When you do this you get the run method that you override as before but you dont get the start method or the other set-up stuff that Thread does behind the scenes. So, what you have to do is create a new Thread and tell it which objects run method to execute - your objects. You create the new Thread and pass it a reference to an object of your class that has implemented Runnable and whos run method you have overridden.
They are, in a sense, two ways of doing the same thing. It allows you the flexability of creating your own hierarchy while still letting you have multithreaded behavior.
hope that helps

arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
dave thanks for the reply.
what i gather is:
public class myThread extends Thread{
// overriding run method of Thread
public void run()
// some code
now acc. to what you say is that i can not subclass myThread...
public class myThread1 extends myThread{
// overriding run of myThread or Thread ??
public void run()

is not allowed.
but why ??
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
No, Dave is not saying that.
If you have a class that you want to make Runnable, and it is not already extending another class then it is OK to make it extend Thread, as in:
class MyThread extends Thread
The problem occurs if you want your class to extend another class. Suppose you are writing a new graphical widget that controls it's own drawing. You might want to extend java.awt.Component
class MyWidget extends Component

But now how do you also turn this into a Thread? A class in Java can have only one parent class, you now can't extend Thread.
Easy make your class implement Runnable
class MyWidget extends Component implements Runnable
Of course, you still have to implement run().
Now to get it running all you have to do is pass an instance of your widget into the constructor for a thread and call start().

Hope this clears things up
[ February 06, 2002: Message edited by: Graeme Brown ]
arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
thanks for clearing up the doubt. Thats in accordance with the concept of Interfaces to enable java have feature of Multiple Inheritance.
thanks again
I agree. Here's the link:
subject: extending thread vs implementing runnable
It's not a secret anymore!