Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread start() method

 
vishal shekhar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one suggest why start method in Thread class is not declared final ?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does it need to be final ?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vishal shekhar:
Can any one suggest why start method in Thread class is not declared final?

Ummm... So it can be overridden?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ummm... So it can be overloaded?



What would we miss if we overloaded start(); Wont calling start() on thread lose its meaning. It should have been final as I think...

Thanks and Regard,
cmbhatt
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chandra Bhatt:
quote: Ummm... So it can be overloaded.

What would we miss if we overloaded start(); Wont calling start() on thread lose its meaning. It should have been final as I think...

Whoops... I see you quoted my mistaken "overloaded" in the few seconds before I corrected it to "overridden" (and it looks like you didn't notice either ). Sorry, I haven't had enough caffeine today.

Anyway, what if we want start() to do something in addition to starting the thread? That is, do some stuff, and then call super.start()?
[ March 31, 2007: Message edited by: marc weber ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I see you quoted my "overloaded" in the minute or two before I corrected it to, "overridden" (and it looks like you didn't notice either ).



Hey marc!
That's perfect guess, I didn't notice; I felt it was "overridden". But we got our answer out of you, "perform some prior operations if required and call super.start()" Great!

Thanks and Regards,
cmbhatt
 
vishal shekhar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for Your replies, especially Marc.
 
vishal shekhar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with your explanation. But can't such a thing be done inside the run method( i mean whatever initializations need be performed )?Is there any particular loss in case by not having start as final?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vishal shekhar:
But can't such a thing be done inside the run method( i mean whatever initializations need be performed )?Is there any particular loss in case by not having start as final?


That goes to the difference between extending Thread and implementing Runnable (which is talked about in detail here). In most cases it's sufficient to implement Runnable (in which case you obviously wouldn't need start to be overridable), but sometimes (rarely) you actually want to alter the behavior of the Thread class, in which case it may be vital to be able to override it.
[ June 29, 2007: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic