• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Thread class is present

 
Gunjan Kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

As all of us know, There are two ways to create a thread in java
1.extends Thread
2. implements Runnable

So if my class is already extending a class and i want to extend Thread class too, in that case , i need to implement Runnable interface.

Now My doubt is, why is Thread class present at all?
why not we did all Thread related work by implementing runnable interface only?

Can you anwser my question please?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface does not contain any implementation. It only specifies what methods a class that implements the interface must provide.

But ofcourse the code to for example start a thread must be implemented somewhere in a class - and class Thread is where that's implemented.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extending Thread class is more like extending the behavior of Thread.But we want to run a process within a separate thread , we do not want to extend / override the behavior of thread.
So ideally its no correct also to extend thread.
 
Harish Tiruvile
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gunjan ,

I think you should extend Thread class is you are creating specialized version of Thread .

In most of the application we are interested in 2 or more thread, since we are not creating new feature to already existing Thread class , Implementing Runnable will be good wrt design

I hope you can get answer for your question by think in term of HAS-A and IS-A relation .
[ August 27, 2007: Message edited by: harish thrivile ]
 
Gunjan Kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I do agree with Jesper , but i have one doubt

Is run method an abstract method in Thread class?
 
Gunjan Kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it, its not an abstract method.

Thanks to all of you
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gunjan Kumar:


Is run method an abstract method in Thread class?


As the Thread class is not abstract , so run should be an empty implementation.You can check the javadoc.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two options have subtly different meanings. If you extend Thread you say your class is a Thread, a thing that manages execution of a task on a separate thread. This is a pretty rare situation because the Thread class works pretty darned well as is. If you implement Runnable you say your class is a kind of task that can be run on a thread. This is much more likely the case, no?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, by extending a Thread class, you become a thread. In other words, you are a thread!

whereas, by implementing the Runnable interface, you are eligible to get the thread behaviours or you know how to have threading operations in your code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic