This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Threads -- Calling start() twice (from constructor and main()) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads -- Calling start() twice (from constructor and main())" Watch "Threads -- Calling start() twice (from constructor and main())" New topic
Author

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

Vigneshwari Chandrasekaran
Greenhorn

Joined: Jan 04, 2013
Posts: 16
[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

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Jan 04, 2013
Posts: 16
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
 
 
subject: Threads -- Calling start() twice (from constructor and main())
 
Similar Threads
calling start() twice on a thread.
confusion with static variable....
Thread Question -JQ++
Threads - join()
Threads problem.