我用debug模式测试了两种单例模式的执行情况:
第一种:
public class TestSingleTon1 {
private static TestSingleTon1 singleton1 = null;
public TestSingleTon1(){
//TODO
System.out.println("constructor methord...");
}
public static synchronized TestSingleTon1 getInstance(){
System.out.println("get instance methord...");
if(singleton1 == null){
singleton1 = new TestSingleTon1();
}
return singleton1;
}
}
第二种:
public class TestSingleTon2 {
private static TestSingleTon2 singleton2 = new TestSingleTon2();
public TestSingleTon2(){
//TODO
System.out.println("constructor methord...");
}
public static TestSingleTon2 getInstance(){
System.out.println("get instance methord...");
return singleton2;
}
}
测试程序:
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("=====TestSingleTon1 wrong test=====");
TestSingleTon1 single1 = new TestSingleTon1();
System.out.println((single1 == null) + " " + single1.hashCode());
TestSingleTon1 single2 = new TestSingleTon1();
System.out.println((single2 == null) + " " + single2.hashCode());
TestSingleTon1 single3 = single2.getInstance();
System.out.println((single3 == null) + " " + single3.hashCode());
System.out.println("=====TestSingleTon1 right test=====");
TestSingleTon1 single1_1 = TestSingleTon1.getInstance();
System.out.println((single1_1 == null) + " " + single1_1.hashCode());
TestSingleTon1 single2_1 = TestSingleTon1.getInstance();
System.out.println((single2_1 == null) + " " + single2_1.hashCode());
System.out.println("=====TestSingleTon2 wrong test=====");
TestSingleTon2 single4 = new TestSingleTon2();
System.out.println((single4 == null) + " " + single4.hashCode());
TestSingleTon2 single5 = new TestSingleTon2();
System.out.println((single5 == null) + " " + single5.hashCode());
TestSingleTon2 single6 = TestSingleTon2.getInstance();
System.out.println((single6 == null) + " " + single6.hashCode());
System.out.println("=====TestSingleTon2 right test=====");
TestSingleTon2 single4_1 = TestSingleTon2.getInstance();
System.out.println((single4_1 == null) + " " + single4_1.hashCode());
TestSingleTon2 single5_1 = TestSingleTon2.getInstance();
System.out.println((single5_1 == null) + " " + single5_1.hashCode());
}
}
测试结果:
=====TestSingleTon1 wrong test=====
constructor methord...
false 1762502123
constructor methord...
false 1193334315
get instance methord...
constructor methord...
false 15735326
=====TestSingleTon1 right test=====
get instance methord...
false 15735326
get instance methord...
false 15735326
=====TestSingleTon2 wrong test=====
constructor methord...
constructor methord...
false 1063563420
constructor methord...
false 130672250
get instance methord...
false 141106670
=====TestSingleTon2 right test=====
get instance methord...
false 141106670
get instance methord...
false 141106670
结果分析:
1.直接使用new,会产生新对象。
2.通过ClassXXX.getInstance,只会找到存在的对象,不存在才会new。
关于第一种的测试情况:
1. private static TestSingleTon1 singleton1 = null;
1.1. public TestSingleTon1(){
//TODO
System.out.println("constructor methord...");
}
2. public TestSingleTon1(){
//TODO
System.out.println("constructor methord...");
}
3.1. public static synchronized TestSingleTon1 getInstance(){
System.out.println("get instance methord...");
if(singleton1 == null){
singleton1 = new TestSingleTon1();
}
return singleton1;
}
3.2. public TestSingleTon1(){
//TODO
System.out.println("constructor methord...");
}
4. public static synchronized TestSingleTon1 getInstance(){
System.out.println("get instance methord...");
if(singleton1 == null){
singleton1 = new TestSingleTon1();
}
return singleton1;
}
5. public static synchronized TestSingleTon1 getInstance(){
System.out.println("get instance methord...");
if(singleton1 == null){
singleton1 = new TestSingleTon1();
}
return singleton1;
}
关于第二种的测试情况:
6.1. private static TestSingleTon2 singleton2 = new TestSingleTon2();
6.2. public TestSingleTon2(){
//TODO
System.out.println("constructor methord...");
}
7. public TestSingleTon2(){
//TODO
System.out.println("constructor methord...");
}
8. public TestSingleTon2(){
//TODO
System.out.println("constructor methord...");
}
9. public static TestSingleTon2 getInstance(){
System.out.println("get instance methord...");
return singleton2;
}
10. public static TestSingleTon2 getInstance(){
System.out.println("get instance methord...");
return singleton2;
}
如果把wrong test都屏蔽掉,则得到如下结果:
=====TestSingleTon1 wrong test=====
=====TestSingleTon1 right test=====
get instance methord...
constructor methord...
false 148669801
get instance methord...
false 148669801
=====TestSingleTon2 wrong test=====
=====TestSingleTon2 right test=====
constructor methord...
get instance methord...
false 1690552137
get instance methord...
false 1690552137
验证不存在才去new的情况
但!是!要!注!意!
真正的单例模式的构造函数用private声明,而不是上面的public,我测试才用public。