[concurrency-interest] a question regarding the double-checked locking

Yechiel Feffer yechielf at gigaspaces.com
Sun Jun 18 07:42:16 EDT 2006

Hi all
will the following double-checked locking code work ?
note that when instance   is assigned a ref to the object, its after the
assigning thread have exited the inner synchronized block,
so the ref dummy_instance must contain a completly initialized object image.
If it works, will replacing the inner synchronized by a  dummy_instance
which is volatile do the job too ?
Are there compiler optimizations which "unify" nested lockes ?
private static Something instance = null;
private  Something dummy_instance = null;

public Something getInstance() {
  if (instance == null) {
    synchronized (some object A) {
        synchronized (some object B) {
          if (instance == null)
            dummy_instance = new Something();
        instance = dummy_instance;
  return instance;

-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060618/bd663bf0/attachment.html 

More information about the Concurrency-interest mailing list