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

About Multithreading in java

Rajesh raju

Joined: Dec 19, 2007
Posts: 2
Is Runnable interface is better or Extending thread is better.Please clarify in detail why it is better.
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Originally posted by Rajesh raju:
Is Runnable interface is better or Extending thread is better.Please clarify in detail why it is better.

Thread class also implements Runnable interface only.
If you see the source code of Thread Class.You can see this line

So i think both the things has the same effect.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

The big difference is reusability.

A thread can be run only once; after it has finished, calling start again will result in an exception. You can reuse a Runnable in multiple threads though, even at the same time.

Of course this still requires creating a thread for each time you need to run it, so in the end the number of objects created will be more or less the same.

How To Ask Questions How To Answer Questions
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Adding on top of what Rob Prime told, a simple way to get another advantage is by implementing an interface my class is free to extend some other business class

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
I have extended Thread when I wanted to make use of the ThreadGroup as a simple way to track the status of working Threads.

sarvesh meens
Ranch Hand

Joined: Mar 31, 2006
Posts: 43

A thread should be extended only when its behaviour has to be extended/modified(which is very rare).
The general requirement is to execute a task/group-of-statements on a seperate thread. Implementing Runnable is the right solution for this requirment.

Refer to Thread FAQ for more details.
I agree. Here's the link:
subject: About Multithreading in java
It's not a secret anymore!