DCL单例模式加入并发控制,保证在多线程的环境下对外只存在一个对象

DCL单例模式
1 构造器私有化 避免外部new构造器
2 提供私有的静态属性 存储对象的地址
3 提供公有的静态方法 获取属性

package cn.com.thread;

public class TestSingleExample {
    private static volatile TestSingleExample instance;
    private TestSingleExample() {

    }
    public static TestSingleExample getInstance() {
        if (instance != null) {
            return instance;
        }
        synchronized (TestSingleExample.class) {
            if (instance == null) {
                instance = new TestSingleExample();
            }
            return instance;
        }
    }
//    //2 提供私有的静态属性
//    private static volatile TestSingleExample instance; //没有new对象 懒汉式
//    //没有volatile其它线程可能会访问一个没有初始化的对象
//    //1 构造器私有化
//    private TestSingleExample() {
//
//    }
//    //3 提供公共的静态方法  获取属性
//    public static TestSingleExample getInstance(long time) {
//        //再次检测
//        if (instance != null) {//避免不必要的同步,已经存在对象
//            try {
//                Thread.sleep(time);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            return instance;
//        }
//        synchronized (TestSingleExample.class) {
//            if (instance == null) {
//                instance = new TestSingleExample();  //发生指令重排 可能返回一个空的对象
//                //new一个对象会做的三件事情
//                //1 开辟空间 2 初始化对象信息 3 返回对象的地址给引用
//            }
//            return instance;
//        }
//    }
//    public static TestSingleExample getInstance2(long time) {
//        if (instance == null) {
//            try {
//                Thread.sleep(time);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            instance = new TestSingleExample();  //发生指令重排 可能返回一个空的对象
//            //new一个对象会做的三件事情
//            //1 开辟空间 2 初始化对象信息 3 返回对象的地址给引用
//        }
//        return instance;
//    }
    public static void main(String[] args) {
        //测试懒汉式单例模式加入并发控制  保证在多线程环境下对外只存在一个对象
        //1 构造器私有化  避免外部new构造器
        //2 提供私有的静态属性  存储对象的地址
        //3 提供公共的静态方法  获取属性
//        Thread t = new Thread(()->{
//            System.out.println(TestSingleExample.getInstance(500));
//        });
//        t.start();
//        System.out.println(TestSingleExample.getInstance(1000));
        Thread t = new Thread(()->{
            System.out.println(TestSingleExample.getInstance());
        });
        t.start();
        System.out.println(TestSingleExample.getInstance());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值