• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Why Thread class is present

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic