发布对象:使一个对象能够被当前范围之外的代码所使用
对象逸出: 一种错误的发布。当一个对象还没有构造完成时,就使它被其它线程可见
@NotThreadSafe public class UnsafePublish { private String[] states = {"a","b","c"}; public String[] getStates(){ return states; } public static void main(String[] args){ UnsafePublish publish = new UnsafePublish(); System.out.println(Arrays.toString(publish.getStates())); //通过公有方法改变类的私有变量,导致需要读取数据时的不确定值 publish.states[0]="d"; System.out.println(Arrays.toString(publish.getStates())); } }
=======================================================================
@NotThreadSafe @NotRecommend public class Escape { private int thisCanBeEscape = 0; public Escape(){ new InnerClass(); } private class InnerClass{ public InnerClass(){ System.out.println(Escape.this.thisCanBeEscape); } } public static void main(String[] args) { new Escape(); } }