this reference to escape problems

// this reference escape 
// constructor has not been completed before the 1, this reference throw itself outwardly, so that other threads to access this reference, and further access to its uninitialized variables, causing problems 
// class 2. Internal External Access uninitialized class member variables 
. 3 // multiple inheritance, the parent class member variable access uninitialized subclass 
public class TestThisEscape { 
    Final int a; 
    int = B. 3; 
    static TestThisEscape obj; 
    public TestThisEscape () { 
        a =. 3 ; 
        B =. 5; 
        // obj = new new TestThisEscape (); // constructor apparent in reference will cause this reference throw escape 
    } 

    public static void main (String [] args) { 
       the Thread ThreadA the Thread new new = (the Runnable new new () { 
            @Override 
            public void RUN () { 
                System.out.println ( "---------------"); 
                obj = new new TestThisEscape ();
                System.out.println ( "============"); 
            } 
        }, "ThreadA"); 

       the Thread ThreadB the Thread new new = (the Runnable new new () { 
            @Override 
            public void RUN () { 
                TestThisEscape obj1 = obj; // obj uninitialized completed, this refers to the use of a null pointer causes 
               // the try { 
                    System.out.println (obj1.a); 
                //} the catch (Exception E) { 
                    System.out.println ( . Thread.currentThread () getName () + "a uninitialized variable"); 
                //} 
                the try { 
                    System.out.println (obj1.b); 
                } the catch (Exception E) { 
                    System.out.println (. Thread.currentThread () getName () + "B uninitialized variable"); 
                } 
            }
        },"threadB");
       threadA.start();
       threadB.start();
    }

}

  

/ ** 
 * escape the this condition: 1. Create inner class constructor. 2. This internal reference the constructor class released out 
 * / 
public class TestInnerClassThisEscape { 
    Final int A; 
    int = B. 3; 
    String C; 
    static TestInnerClassThisEscape obj; 
    public TestInnerClassThisEscape () { 
        new new the Thread (the Runnable new new () { 
            @ override 
            public void RUN () { 
                System.out.println (TestInnerClassThisEscape.this.a); 
                System.out.println (TestInnerClassThisEscape.this.b); 
                System.out.println (TestInnerClassThisEscape.this.c); 
            } 
        }). Start (); 
        the try {  
            the Thread.sleep (1000);
        } the catch (InterruptedException E) {
            e.printStackTrace();
        }
        a= 3;
        b= 5;

    }

    public static void main(String[] args) {
        new TestInnerClassThisEscape();
    }

}
public class TestThisPC {
    public static void main(String[] args) {
       new Child();
    }
}
class Parent{
    Parent(){
        print();
    }
    void print(){
        System.out.println("----------");
    }
}


class Child extends Parent{
    int a = 5;
    void print(){
        System.out.println("-----------"+ a);
    }
     Child(){
        super();
        print();
    }
}

  

Solution: 
1. Before calling initialization

Guess you like

Origin www.cnblogs.com/htkj/p/10932649.html
Recommended