Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

SCJP / strange behaviour when using Thread-Constructor

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?

Posts: 22028
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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 inside your run() method.
Ranch Hand
Posts: 111
  • 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.
Get me the mayor's office! I need to tell her about this tiny ad:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic