IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

大话设计模式之单例模式

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

单例模式

  一个类有且只有一个实例;

特点

  • 1、单例类只能有一个实例。
  • 2、单例类必须自己创建自己的唯一实例。
  • 3、单例类必须给所有其他对象提供这一实例。

单例模式的几种实现方式

一:饿汉式

public class Singleton {
    private static Singleton singleton = new Singleton();

    private Singleton(){}

    public static Singleton getInstance(){

        return singleton;
    }

}  

饿汉式是指在类加载的过程中就完成了实例化;避免了多线程问题,因此属于线程安全;但是没有达到 lazy loading 的效果,所以可能存在内存浪费的情况。

二:懒汉式(线程不安全)

public class Singleton {
    private static Singleton singleton;

    private Singleton(){}

    public static Singleton getInstance(){
        if(singleton==null){
            singleton = new Singleton();
        }
        return singleton;
    }

}

 这种实现最大的问题就是不支持多线程。因为没有加锁 synchronized,线程不安全 

三:懒汉式(线程安全)

public class Singleton {
    private static Singleton singleton;

    private Singleton(){}

    public static synchronized Singleton getInstance(){
        if(singleton==null){
            singleton = new Singleton();
        }
        return singleton;
    }

}

 解决上述线程不安全的方法就是在方法上加个同步(synchronized),因此也导致了效率太低,所以不推荐使用。

四:双重锁定

public class Singleton {
    private static volatile Singleton singleton;

    private Singleton(){}

    public static Singleton getInstance(){
        if(singleton==null){//防止重复锁定
            synchronized(Singleton.class){//Singleton.class表示锁定的是Singleton的class对象;如果是this则表示当前对象
                singleton = new Singleton();
            }
        }
        return singleton;
    }

}

  这种方式采用双锁机制,安全且在多线程情况下能保持高性能;

文章永久链接:https://tech.souyunku.com/?p=39904


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(80) 打赏



未经允许不得转载:搜云库技术团队 » 大话设计模式之单例模式

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367