[concurrency-interest] synchronized constructors
yshavit at akiban.com
Fri Dec 16 09:38:19 EST 2011
On Fri, Dec 16, 2011 at 7:24 AM, Ruslan Cheremin <cheremin at gmail.com> wrote:
> As far, as I understand, topic starter does not want to elide locks
> from other threads. Contrary, he wants to protect _all_ methods of his
> object with locks -- making them completely thread-safe, even in case
> of data-race-based publication. And he asks, is protecting constructor
> does this job, and if it is - why synchronized constructor is
> prohibited in JLS?
Several people have made the claim that you could see partially initialized
state even if the constructor were synchronized, and I don't see how this
could be. Yes, you could assign MyPoint myUnsafeRef before MyPoint()
finishes -- but if you ever tried to *use* myUnsafeRef, you would run into
a synchronized block which would then ensure that the constructor
happened-before whatever method you're writing. Seems to me you should see
the fully-thread-safe, not-partially-initialized object at that point.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Concurrency-interest