[concurrency-interest] synchronized constructors

Vitaly Davidovich vitalyd at gmail.com
Sun Dec 18 16:56:23 EST 2011


Hi Martin,

This seems a bit dubious since you can just mark your fields final.  What's
the advantage of the dummy assignment? In Roland's case, he couldn't assign
one of the fields in the constructor because it has to be done via a setter
post construction but before publishing.

Regards,

Vitaly

On Sun, Dec 18, 2011 at 4:22 PM, Martin Buchholz <martinrb at google.com>wrote:

>
>
> On Sun, Dec 18, 2011 at 13:02, Roland Kuhn <rk at rkuhn.info> wrote:
>
>> Yes, I would very much welcome a simple way achieving a fence, as you
>> might imagine ;-)
>>
>
> Many of us here would like to see a way to safely publish an object.
>
> ---
>
> Here's a solution no one has mentioned, but ought to be bullet-proof:
>
> public class MutableSafePublication {
>     private int i;
>     private Object x;
>     private final MutableSafePublication safePublicationThis;
>     private int i() { return safePublicationThis.i; }
>     private Object x() { return safePublicationThis.x; }
>     public MutableSafePublication() {
>         i = 42;
>         x = new Object();
>         safePublicationThis = this;
>     }
>     // use i() and x() consistently in the implementation
> }
>
>


-- 
Vitaly
617-548-7007 (mobile)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20111218/834f7d0b/attachment.html>


More information about the Concurrency-interest mailing list