写一个SingleTon,(饿最终、懒同步)
1.饿汉式:
public class SingleTon { private SingleTon(){ } private final static SingleTon instance = new SingleTon(); public static SingleTon getInstance(){ return instance; } }
2.懒汉式:
public class SingleTon { private SingleTon(){ } private static SingleTon instance = null; public static synchronized SingleTon getInstance(){ if(instance == null) instance = new SingleTon(); return instance; } }
3.枚举:
public enum SingleTon { instance; }