[concurrency-interest] Double Checked Locking in OpenJDK

Chris Povirk cpovirk at google.com
Wed Aug 15 20:50:45 EDT 2012


>> 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?
}


More information about the Concurrency-interest mailing list