• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP / strange behaviour when using Thread-Constructor

 
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
Pie
Posts: 20494
54
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic