本帖最后由 zhang63 于 2014-6-3 14:25 编辑
- //方法1
- class Singleton1
- {
- private static Singleton1 singleton = null;
- private Singleton1() {}
-
- public static Singleton1 getInstance()
- {
- if (singleton == null)
- {
- synchronized (Singleton1.class)
- {
- if (singleton == null)
- singleton = new Singleton1();
- }
- }
- return singleton;
- }
- }
- //方法2
- class Singleton
- {
- private static Singleton singleton = null;
- private Singleton() {}
- public static synchronized Singleton getInstance()
- {
- if(singleton==null)
- {
- singleton = new Singleton();
- }
- return singleton;
- }
- }
复制代码 |
|