GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized constructors" Watch "synchronized constructors" New topic
Author

synchronized constructors

Vidyasagar Guduru
Greenhorn

Joined: Apr 17, 2006
Posts: 26
If we use a synchronized constructor like below, compiler will not agree.



but using a synchronized block is legal




Why the compiler is allowing the second one and not the first one.
[ December 18, 2006: Message edited by: Vidyasagar Guduru ]
Bipin Velkur
Greenhorn

Joined: Dec 18, 2006
Posts: 2
This is the language feature. The reason why this limitation is placed is because:

Two threads that call the same constructor concurrently operate on two distinct pieces of memory for two distinct instances of the same class.

If there is a mutually shared resource, you must synchronize on that resource in order to avoid conflicts.

Hope this clears the thing up.
Bipin Velkur
Greenhorn

Joined: Dec 18, 2006
Posts: 2
Just did a google and here is an article if you want to read more about it:

http://www-128.ibm.com/developerworks/java/library/j-praxis/pr46.html


P.S: Parts of my post were taken from this article
Andrea Z�rcher
Greenhorn

Joined: Oct 06, 2006
Posts: 10
Try this: web page


SCJP 5.0
Vidyasagar Guduru
Greenhorn

Joined: Apr 17, 2006
Posts: 26
Thanks for the links.
 
GeeCON Prague 2014
 
subject: synchronized constructors