This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads -- Calling start() twice (from constructor and main())

 
Vigneshwari Chandrasekaran
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Added code tags - see UseCodeTags for details]

In this code, start() is called twice (one inside constructor & another from main()) , but on the same instance.
Actually in case of calling start() more than once, we will be getting IllegalMonitorStateException right?
Then how is it possible to get the code executed with output "main22".
I am really confused about this output. Why is this such exceptional case for calling a method from constructor?
Please anybody explain.
Thanks!!
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vigneshwari. Welcome to the Ranch!

Are you sure they're being called on the same instance? How many Thread objects do you think you are creating in that code?
 
Vigneshwari Chandrasekaran
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Hi Vigneshwari. Welcome to the Ranch!

Are you sure they're being called on the same instance? How many Thread objects do you think you are creating in that code?


I just got clear with it.. My friend helped me.
The start() call within constructor is executed by main Thread. And that in main() is by the instance created. Hence no exception..

Thanks for the reply
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic