栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java单例模式的9种实现方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java单例模式的9种实现方式

文章目录
  • Java单例模式的9种实现方式
        • 一.饿汉式
        • 二.懒汉式,线程不安全
        • 三.懒汉式,线程安全
        • 四.双检锁/双重校验锁(DCL,即 double-checked locking)
        • 五.静态内部类
        • 六.枚举
        • 七.双检锁/双重校验锁(DCL,即 double-checked locking)(加volatile)
        • 八.使用ThreadLocal实现单例
        • 九.使用CAS锁实现单例
  • 我的学习论坛

Java单例模式的9种实现方式

一.饿汉式
public class Singleton {  
    private static Singleton instance = new Singleton();  
    private Singleton (){}  
    public static Singleton getInstance() {  
    return instance;  
    }  
}

JDK中,java.lang.Runtime就是典型的使用饿汉式的单例模式

二.懒汉式,线程不安全
public class Singleton {  
    private static Singleton instance;  
    private Singleton (){}  
  
    public static Singleton getInstance() {  
    if (instance == null) {  
        instance = new Singleton();  
    }  
    return instance;  
    }  
}
三.懒汉式,线程安全
public class Singleton {  
    private static Singleton instance;  
    private Singleton (){}  
    public static synchronized Singleton getInstance() {  
    if (instance == null) {  
        instance = new Singleton();  
    }  
    return instance;  
    }  
}
四.双检锁/双重校验锁(DCL,即 double-checked locking)

JDK 版本:JDK1.5 起

public class Singleton {  
    private volatile static Singleton singleton;  
    private Singleton (){}  
    public static Singleton getSingleton() {  
    if (singleton == null) {  
        synchronized (Singleton.class) {  
        if (singleton == null) {  
            singleton = new Singleton();  
        }  
        }  
    }  
    return singleton;  
    }  
}
五.静态内部类
public class Singleton {  
    private static class SingletonHolder {  
    private static final Singleton INSTANCE = new Singleton();  
    }  
    private Singleton (){}  
    public static final Singleton getInstance() {  
    return SingletonHolder.INSTANCE;  
    }  
}
六.枚举

JDK 版本:JDK1.5 起

public enum Singleton {  
    INSTANCE;  
    public void whateverMethod() {  
    }  
}
七.双检锁/双重校验锁(DCL,即 double-checked locking)(加volatile)

JDK 版本:JDK1.5 起

public class Singleton {  
	# 使用volatile设置内存屏障,确保线程访问资源可见性。
    private volatile static Singleton singleton;  
    private Singleton (){}  
    public static Singleton getSingleton() {  
    if (singleton == null) {  
        synchronized (Singleton.class) {  
        if (singleton == null) {  
            singleton = new Singleton();  
        }  
        }  
    }  
    return singleton;  
    }  
}
八.使用ThreadLocal实现单例
public class Singleton {  
	// 用空间换时间
	private static final ThreadLocal tl = new ThreadLocal();
    @Override
    protected Singleton initialValue() {
        return new Singleton();
    }

    private Singleton() {}

    public static Singleton getInstance() {
        return tl.get();
    }
}
九.使用CAS锁实现单例
private static final AtomicReference INSTANCE = new AtomicReference();

    private Singleton() {
    }

    public static final Singleton getInstance() {
        for (; ; ) {
            Singleton current = INSTANCE.get();
            if (current != null) {
                return current;
            }

            current = new Singleton();
            if (INSTANCE.compareAndSet(null, current)) {
                return current;
            }
        }
    }
我的学习论坛

HandsomeForum:用Java编写的学习论坛,打造我们自己的圈子!(http://huangjunjie.vip:66)
文章链接:http://huangjunjie.vip:66/blog/read/8xb2zexvrhiii3anpq

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/999695.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号