• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
John Drulo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 3
  • Mark post as helpful
  • send pies
  • 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: http://www.coderanch.com/how-to/java/ExtendingThreadVsImplementingRunnable. So I think the less time you spend thinking about it (especially as a beginner), the better.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!