题二:写一个单例模式

/**
 * 2、写一个单例模式
 */
public class Test2 {
    public static void main(String[] args) {
        System.out.println(Singleton.getSingleton().hashCode());
    }
}
class Singleton{

    private static Singleton singleton = new Singleton();
    private Singleton(){}
    public static Singleton getSingleton(){
        return singleton;
    }
}

饿汉模式,秀操作的可以来套线程锁。

猜你喜欢

转载自www.cnblogs.com/flying607/p/9004131.html