java 单例模式实现的5种方式

第一种:饿汉式

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 宸濯
 * Date: 2021/08/18 8:21
 * Description:单例模式的设计(饿汉式)
 * 1.构造方法私有化
 * 2.在静态语句块实例化
 * 3.提供调用实例对象的方法
 * 4.空间换时间,不管有没有调用方法,实例都创建了
 * Version: V1.0
 */
public class SingletonOne {

    private static final int THREADS=100;
    private static SingletonOne instance;
    /**
     * 构造方法私有化
     */
    private SingletonOne(){}

    static {
        instance=new SingletonOne();
    }

    public static SingletonOne getInstance(){
        return instance;
    }

    public static void main(String[] args) {
        for (int i=0;i<THREADS;i++){
            new Thread(
                    ()-> System.out.println(getInstance().hashCode())
            ).start();
        }
    }
}

第二种:懒汉式

/**
 * Created with IntelliJ IDEA.
 * @author: 宸濯
 * Description:单例模式设计(懒汉式)
 * 1.构造方法私有化
 * 2.在调用时判断是否实例化
 * 3.时间换空间,调用方法时创建实例对象
 */
public class SingletonTow {

    private static SingletonTow instance;
    private static final int THREADS=100;
    private SingletonTow(){}

    public static SingletonTow getInstance(){

        if (instance==null){
            instance=new SingletonTow();
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i=0;i<THREADS;i++){
            new Thread(
                    ()-> System.out.println(getInstance().hashCode())
            ).start();
        }
    }
}

第三种:双检锁式

/**
 * Created with IntelliJ IDEA.
 * @author: 宸濯
 * Description:单例模式设计(双检锁懒汉式)
 * 1.构造方法私有化
 * 2.在调用时判断是否实例化
 * 3.时间换空间,调用方法时创建实例对象
 */
public class SingletonTow {
    /**
     * volatile修饰符防止指令重排序
     */
    private static volatile SingletonTow instance;
    private static final int THREADS=100;
    private SingletonTow(){}

    public static SingletonTow getInstance(){

        if (instance==null){
            synchronized (SingletonTow.class){
                //双重判定,防止高并发,不过会导致指令重排序
                if (instance==null){
                    instance=new SingletonTow();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i=0;i<THREADS;i++){
            new Thread(
                    ()-> System.out.println(getInstance().hashCode())
            ).start();
        }
    }
}

java并发编程:volatile关键字解析

第四种:静态内部类式

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 宸濯
 * Description:设计单例模式
 * 1.构造方法私有化
 * 2.静态内部类实例化对象
 * 3.静态内部类只会被加载一次,类加载的初始化阶段是单线程的,没有高并发带来的冲突
 */
public class SingletonThree {

    /**
     * 声明线程数
     */
    private static final int THREADS=100;
    private static class Inner{
        private static final SingletonThree INSTANCE=new SingletonThree();
    }

    private SingletonThree(){}

    public static SingletonThree getInstance(){
        return Inner.INSTANCE;
    }

    public static void main(String[] args) {
        for (int i=0;i<THREADS;i++){
            new Thread(
                    ()-> System.out.println(SingletonThree.getInstance().hashCode())
            ).start();
        }
    }
}

第五种:枚举类型

/**
 * Created with IntelliJ IDEA.
 * @author : 宸濯
 */
public enum  SingletonFour {

    //枚举类型设计单例模式
    SINGLETON_FOUR;

    public void test(){
        System.out.println("hello world");
    }

}

public class Main {
    public static void main(String[] args) {
        SingletonFour.SINGLETON_FOUR.test();
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值