Query on synchronization in DCL idiom /Singleton
===

   // Broken multithreaded version "Double-Checked Locking" idiom
     <pre>
     class Foo { 
       private Helper helper = null;
       private volatile booleab check=true;
       public Helper getHelper() {
        if (helper == null) 
          synchronized(this) {
             if (helper == null) 
             helper = new Helper();
          }    
         return helper;
        }
       public void print(){
         sysout(Object reference:"+helper)
        }
      public void setCheck(){
       check=false;
       }
      }
     </pre>
   The above DCL is failed because of this statement <b>helper = new Helper();</b>       {1.allocateMem(),2.callConstructors(), 3.Assign reference} 
JIT/JVM reorders -- 132 instead of 123

1)  Thread (T1) executing synchronized block , so T1 has lock on FOO object 
After executing 1,3 statements T2(new thread) is scheduled so T1 goes to WAIT state  
 
<b>How T2 can access  getHelper() because T1 is still Holding LOCK on FOO Object </b>(i.e instance fields of FOO, Super classes & it's declared as synchronize so all memory writes,reads directly goes to main memory )?


2a) volatile : it provides visibility & happens before relationship( write-read)


increment/ decrements on volatile variable is not atomic ---From Jeremy's blog

<b>what about reading and assignment to a volatile variable --> atomic?</b> 

    volatile int i;
    i=0; 
    int j=i;

 
2b)private volatile Helper helper = null; --> how it provides correct solution (if T1 is interrupted by T2 after 13 steps)


3a)<b>T1 is inside Synchronize block and preempted by T3, isT3 allowed to call print(), setCheck() ?</b>


3b)Is it possible to call T1 non-synchronized method(accessing  instance variables), T2 non- synchronized method (with sysout("hI")) , T3 synchronized method  at simultaneously(t1-t3-t2-t3...) 






