• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running Threads without using a Runnable implementation, How to do it? Just out of curiosity :) ?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw the following excerpt in HeadFirstJava, 2nd Edition pg 500
"I've seen examples that don't use a separate
Runnable Implementation, but Instead Just make a
subclass of Thread and override the Thread's runO
method. That way,you call the Thread's no--arg
constructor when you make the new thread;
Thread t = new Thread(); 1/no Runnable"

Can someone please provide a simple code that demonstrates the same. I haven't fully understood what is said in the text above. I have a hunch that the last line is wrong and it should be
Thread t = new <Whatever class extends Thread class and over rides its run method>()

Am I correct or wrong
 
Rancher
Posts: 43081
77
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The circumstances where it's appropriate to extend Thread are very rare, and with the java.util.concurrent framework there may well be none at all: https://coderanch.com/how-to/java/ExtendingThreadVsImplementingRunnable. So I think the less time you spend thinking about it (especially as a beginner), the better.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can someone please provide a simple code that demonstrates the same. I haven't fully understood what is said in the text above. I have a hunch that the last line is wrong and it should be
Thread t = new <Whatever class extends Thread class and over rides its run method>()

Am I correct or wrong


You are correct but as Ulf has already said you will probably never need to override Thread and in all my years of using Java I've yet to see a valid case for overriding Thread. I seen plenty of cases where a Runnable should have been used instead of overriding Thread but no doubt there are some valid scenarios.
reply
    Bookmark Topic Watch Topic
  • New Topic