aspose file tools*
The moose likes Java in General and the fly likes Why Thread class is present Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why Thread class is present" Watch "Why Thread class is present" New topic
Author

Why Thread class is present

Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
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?


Gunjan Kumar
SCJP 1.5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
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 ]

Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
Hi

I do agree with Jesper , but i have one doubt

Is run method an abstract method in Thread class?
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
I got it, its not an abstract method.

Thanks to all of you
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


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]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Thread class is present