[concurrency-interest] synchronized constructors

Martin Buchholz martinrb at google.com
Sun Dec 18 16:22:55 EST 2011


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
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20111218/d308bf4d/attachment.html>


More information about the Concurrency-interest mailing list