This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Difference in using Runnable interface and Thread class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Difference in using Runnable interface and Thread class" Watch "Difference in using Runnable interface and Thread class" New topic
Author

Difference in using Runnable interface and Thread class

Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Hello friends,

People say that if you just want to create thread then go for Runnable.

Here is the statement

"The Thread class has a lot more overhead (in terms of methods and attributes) and most of the time the developers only need to use the run() method.".

Now my doubt is what is this overhead ? Is it in terms of memory ?
Please help me

Waiting for reply.

Regards
Rohit.
Sunetra Sen
Ranch Hand

Joined: Feb 17, 2005
Posts: 43
Hi,
Other than the overhead stuff it is also a fact that most of the times in actual application your class(which you want to make a thread)will inherit from other classes.
So as multiple extends cannot be used in java it is always advisable to implement the Runnable interface.Even if your class doesn't extend any other class presently ,you keep your option open to do that at a future date,by implementing Runnable.

The following is straight out of the API:
In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to Threads forum. You might want to do a search there, as this is a very frequently asked question...


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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference in using Runnable interface and Thread class
 
Similar Threads
Why java has 2 ways to create thread ?
extends thread as well as implements Runnable
when to use runnable?
extend or implement?
Thread class or Runnable