使用静态内部类来实现单例多线程

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * @author 玖洲林
 * Create Time 2019-08-17 下午 06:41
 */
public class Singleton implements Serializable {

    private static class SingletonHandler {
        private static final Singleton INSTANCE = new Singleton();
    }

    private Singleton() {
    }

    public static Singleton getInstance() {
        return SingletonHandler.INSTANCE;
    }

    private Object readResolve() throws ObjectStreamException {
        //反序列化
        return SingletonHandler.INSTANCE;
    }

}

发表评论