• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Extending Thread Class vs Implements Runnable

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 95
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14502
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 95
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread is a concrete class.
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic