并发编程-完美单例模式 时间:2018/11/1

class God
{
    private static God instance = null;
    private static object locker = new object();
    private God(){} //构造函数私有
    public static God GetInstance()
    {
        if(instance == null) 
        {
            /*此时可能会有别的线程去创建对象*/
            lock(locker)
            {
                if(instance == null) //这里再次判断
                    instance = new God();
            }
        }
        return instance;
    }
}

猜你喜欢

转载自blog.csdn.net/u014793452/article/details/83618456