aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread start() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread start() method" Watch "Thread start() method" New topic
Author

Thread start() method

vishal shekhar
Greenhorn

Joined: Feb 03, 2007
Posts: 7
Can any one suggest why start method in Thread class is not declared final ?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Why does it need to be final ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

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


cmbhatt
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Feb 28, 2007
Posts: 1707

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

Joined: Feb 03, 2007
Posts: 7
Thanks for Your replies, especially Marc.
vishal shekhar
Greenhorn

Joined: Feb 03, 2007
Posts: 7
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
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
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 ]

Ping & DNS - my free Android networking tools app
 
Consider Paul's rocket mass heater.
 
subject: Thread start() method