单例实现-记录

第一种:

public class InstanceA {

private static InstanceAinstanceA;

private InstanceA() {

}

public static InstanceA getInstance() {

if (instanceA ==null) {

synchronized (InstanceA.class){

if (instanceA ==null){

instanceA =new InstanceA();

}

}

}

return instanceA;

}

}

第二种:

public class InstanceA {

private InstanceA() {

}

private static class TestHolder {

private static InstanceAinstanceA =new InstanceA();

}

public static InstanceA getInstance() {

return TestHolder.instanceA;

}

}

你可能感兴趣的:(单例实现-记录)