• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference in using Runnable interface and Thread class

 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads forum. You might want to do a search there, as this is a very frequently asked question...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic