[concurrency-interest] Double Checked Locking in OpenJDK

David Holmes davidcholmes at aapt.net.au
Wed Aug 15 20:59:25 EDT 2012


Chris,

The critical part is that you are synchronizing on the same object - the
vector. That is what gives the correct happens-before relationship.

David

> -----Original Message-----
> From: concurrency-interest-bounces at cs.oswego.edu
> [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Chris
> Povirk
> Sent: Thursday, 16 August 2012 10:51 AM
> To: concurrency-interest at cs.oswego.edu
> Subject: Re: [concurrency-interest] Double Checked Locking in OpenJDK
>
>
> >> If a use case does exist, there is a way to do it without
> >> synchronizing constructor:
> >>
> >>     local_var = new Vector();  // not synchronized
> >>     local_var.size();  // call a synchronized method
> >>     shared_var = local_var;  // unsafe publication
> >>
> >> therefore synchronized constructor still isn't necessary.
> >
> > The above is turning unsafe publication into safe-publication.
>
> I'm still confused about this solution.  "Call a synchronized method"
> feels to me like "write synchronized (x) {}": The "roach motel" model
> suggests that it should work, but I don't understand how it introduces
> a happens-before relationship.  I can see one difference between the
> two: With "call a synchronized method," we're at least entering a
> synchronized block in both the reader thread and the writer thread,
> but my impression is that that's not enough, either.  Or would this
> really work?
>
> local = new Vector();
> synchronized (local) {}
> shared = local;
>
> // other thread:
> local = shared;
> if (local != null) {
>   synchronized (local) {}
>   // safe to use local now?
> }
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>



More information about the Concurrency-interest mailing list