Forums Register Login

SCJP / strange behaviour when using Thread-Constructor

+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 997 times.
Similar Threads
starting a thread in a constructor
Using super and this in the constructor together
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:40:55.