短视频系统源码,几种常见的单例模式

短视频系统源码,几种常见的单例模式实现的相关代码

前言:直接介绍几种线程安全的且我觉得还比较不错的方式:

1.

 


public class Singleton  
{  
    private static Singleton instance = new Singleton();  
    public static Singleton getInstance()  
    {  
        return instance ;  
    }  
}  

​2、

 


public class Singleton02  
{  
    private static Singleton02 instance;  
    public static Singleton02 getInstance()  
    {  
        if (instance == null)  
        {  
            synchronized (Singleton02.class)  
            {  
                if (instance == null)  
                {  
                    instance = new Singleton02();  
                }  
            }  
        }  
        return instance;  
    }  
}  

3、使用一个持有类,主要是为了不在初始化的时候加载

 


public class Singleton04  
{  
    private static final class InstanceHolder  
    {  
        private static Singleton04 INSTANCE = new Singleton04();  
    }  
    public static Singleton04 getInstance()  
    {  
        return InstanceHolder.INSTANCE;  
    }  

 

以上就是 短视频系统源码,几种常见的单例模式实现的相关代码,更多内容欢迎关注之后的文章

 

posted @ 2022-05-11 14:09  云豹科技-苏凌霄  阅读(57)  评论(0)    收藏  举报