• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Extending Thread Class vs Implements Runnable

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I can create a class extending Thread.class to implement own thread mechanism or i can create a thread class implements Runnable

1) When to create a class by extending Thread.class? In which situation or use case we need to go for it
2) Many examples extends Thread.class and many uses Runnable. Not sure what is the best pratice

Thanks
 
Rancher
Posts: 4614
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally?
If you're not changing how a Thread functions (ie not doing anything other than implementing the run method) then you're not really extending the Thread class and you should simply implement Runnable.
 
Saloon Keeper
Posts: 6515
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See https://coderanch.com/wiki/660195/Extending-Thread-Implementing-Runnable for a FAQ entry that basically says what Dave said. Note that there are more capable versions of Runnable like Callable and Future.
 
Joseph Michael
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand now..Can you give me few real time examples where extending Thread has been implemented instead of using Runnable?
 
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joseph Michael wrote:Hi,

I can create a class extending Thread.class to implement own thread mechanism or i can create a thread class implements Runnable

1) When to create a class by extending Thread.class? In which situation or use case we need to go for it
2) Many examples extends Thread.class and many uses Runnable. Not sure what is the best pratice

Thanks

suppose you have a class, and you extends thread class to make your class as thread class, it's ok but if you want to extends one more class to the same thread class so not possible because multiple inheritance not possible example
Class A extends Thread{} // ok thread class A
Class A extends Thread, B{} // not ok
Class B{}
But where as implementing runnable interface OK example...
Class A extends B  implements Runnable{}
 
Saloon Keeper
Posts: 12142
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joseph Michael wrote:Can you give me few real time examples where extending Thread has been implemented instead of using Runnable?


There are none. The Thread class should have been final.

sohail hussain wrote:you extends thread class to make your class as thread class, it's ok


It's not okay. Never extend Thread.

In fact, don't even use the Thread class directly. Depend on an ExecutorService instead.
 
sohail hussain
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Joseph Michael wrote:Can you give me few real time examples where extending Thread has been implemented instead of using Runnable?


There are none. The Thread class should have been final.

sohail hussain wrote:you extends thread class to make your class as thread class, it's ok


It's not okay. Never extend Thread.

In fact, don't even use the Thread class directly. Depend on an ExecutorService instead.

then how can we make concrete class as thread class without extending  please explain i am to confused
 
Dave Tolls
Rancher
Posts: 4614
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread is a concrete class.
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic