This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Benefits of Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Benefits of "extends Thread" vs "implements Runnable"" Watch "Benefits of "extends Thread" vs "implements Runnable"" New topic
Author

Benefits of "extends Thread" vs "implements Runnable"

landon manning
Ranch Hand

Joined: Nov 20, 2000
Posts: 47
What is the benefit of having a class extend Thread vs having a class implement Runnable?
<code>
public class T extends Thread{

public static void main( String[] args ){

//What is the benefit of doing this...
new T().start();

//...as opposed to this. (Or vice versus)
new Thread( new R() ).start();
}

public void run(){
System.out.println("In T");
}

}
class R implements Runnable{

public void run(){
System.out.println("In R");
}
}
</code>
Thanks,
Landon Manning
vishad patel
Greenhorn

Joined: Dec 05, 2000
Posts: 17
hi ,friend
As you kbnow that java does not support multiple inheritance so that when you can write coding for applet then you extends the applet class ,in such a case you can't extends the Thread.so that to avoid this kinds of problem you have to implements the Runnable.
I think this is sufficient for your understanding.
Thanks .
if i am wrong then mail me.
vishad20002001@yahoo.com
Bye.
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Landon,
The only good reason to derive a subclass from Thread is to extend its behavior - just like any other class. It's usually a better idea to implement Runnable.
Delegation, when it satisfies the requirements, is a more flexible solution than specialization. While it's true that overriding run() is in some sense extending Thread's behavior, it's more in keeping with good OO practice to derive a Thread subclass only if you need to modify some more basic aspect. In the simple example programs you see in most books (where the author often just makes the main class a subclass of Thread), the two approaches are equivalent. In larger programs, this kind of failure to make the code reflect the design abstractions often leads to trouble.
Just my opinion, of course.
Jerry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Benefits of "extends Thread" vs "implements Runnable"
 
Similar Threads
regarding threads
When both Thread and Runnable have public void run
run Vs. start
Problem in thread
clojure functors vs normal methods