This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SCJP / strange behaviour when using Thread-Constructor  RSS feed

 
Guido Ranft
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought when I create an extended thread instance with a constructor object implementing runnable
the run-method of the constructor instance is used. Instead the overriden method is used. Why?

 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch! Could you please UseCodeTags next time?

Anyway, when a Thread is started, its run() method is executed in a new thread. The default behaviour is to call the run() method of its Runnable. Since you've overridden the method you don't get that behaviour anymore. A simple fix is to call super.run() inside your run() method.
 
Istvan Kovacs
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are asking the wrong question. The question you should be asking is "Why does Thread run the supplied Runnable?". (Tip: read its source code.) Once you figure that out, you'll know the answer to your question.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!