美文网首页
5、Java并发编程入门与高并发面试-安全发布对象

5、Java并发编程入门与高并发面试-安全发布对象

作者: 安安汐而 | 来源:发表于2020-05-23 10:58 被阅读0次

慕课网 Jimin老师 Java并发编程入门与高并发面试 学习笔记
Java并发编程入门与高并发面试

◆发布对象:使一个对象能够被当前范围之外的代码所使用
◆对象逸出:一种错误的发布。当-一个对象还没有构造完成时,就使它被其他线程所见

package com.huhao.concurrency.example.publish;

import com.huhao.concurrency.annoations.NotRecommend;
import com.huhao.concurrency.annoations.NotThreadSafe;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@NotThreadSafe
@NotRecommend
public class Escape {
    private int thisCanBeEscape = 0;

    public Escape() {
        new InnerClass();
    }

    private class InnerClass {
        public InnerClass() {
            log.info("{}", Escape.this.thisCanBeEscape);
        }
    }

    public static void main(String[] args) {
        new Escape();
    }

}

发布安全对象

◆在静态初始化函数中初始化-个对象引用
◆将对象的引用保存到volatile类型域或者AtomicReference对象中
◆将对象的引用保存到某个正确构造对象的final类型域中
◆将对象的引用保存到一 个由锁保护的域中

package com.huhao.concurrency.example.singleton;

import com.huhao.concurrency.annoations.NotThreadSafe;

/**
 * 懒汉模式
 * 单例的实例在第一次使用时候进行创建
 * <p>
 * but:只能单线程中是单例的,多线程调用时可能会创建多个
 */
@NotThreadSafe
public class SingletonExample {
    /**
     * 单例构造器
     * 必须私有,不能外部调用
     */
    private SingletonExample() {
    }

    //单例对象
    private static SingletonExample instance = null;

    //静态的工程方法
    public static SingletonExample getInstance() {
        if (instance == null) {
            instance = new SingletonExample();
        }
        return instance;
    }
}

package com.huhao.concurrency.example.singleton;

import com.huhao.concurrency.annoations.ThreadSafe;

/**
 * 饿汉模式
 * 单例的实例在类装载时候进行创建
 * <p>
 * but:构造器中如果处理的东西比较多,则该类会加载比较慢,
 * 产生性能问题
 */
@ThreadSafe
public class SingletonExample2 {
    /**
     * 单例构造器
     * 必须私有,不能外部调用
     */
    private SingletonExample2() {
    }

    //单例对象
    private static SingletonExample2 instance = new SingletonExample2();

    //静态的工程方法
    public static SingletonExample2 getInstance() {
        return instance;
    }
}

package com.huhao.concurrency.example.singleton;

import com.huhao.concurrency.annoations.NotRecommend;

/**
 * 懒汉模式
 * 单例的实例在第一次使用时候进行创建
 * <p>
 * getInstance方法设置synchronized,则多线程下只会有1个执行
 * but:不推荐,性能会比较低
 */
@NotRecommend
public class SingletonExample3 {
    /**
     * 单例构造器
     * 必须私有,不能外部调用
     */
    private SingletonExample3() {
    }

    //单例对象
    private static SingletonExample3 instance = null;

    //静态的工程方法
    public static synchronized SingletonExample3 getInstance() {
        if (instance == null) {
            instance = new SingletonExample3();
        }
        return instance;
    }
}
package com.huhao.concurrency.example.singleton;

import com.huhao.concurrency.annoations.NotThreadSafe;

/**
 * 懒汉模式 - 双层同步锁单例模式
 * 单例的实例在第一次使用时候进行创建
 */
@NotThreadSafe
public class SingletonExample4 {
    /**
     * 单例构造器
     * 必须私有,不能外部调用
     */
    private SingletonExample4() {
    }

    // 1、memory = allocate() 分配对象的内存空间
    // 2、ctorInstance() 初始化对象
    // 3、instance = memory设置ins tance指向刚分配的内存

    // JVM和cpu优化,发生了指令重排

    // 1、memory = allocate() 分配对象的内存空间
    // 3、instance = memory 设置ins tance指向刚分配的内存
    // 2、ctorInstance()初始化对象|


    //单例对象
    private static SingletonExample4 instance = null;

    //静态的工程方法
    public static SingletonExample4 getInstance() {
        if (instance == null) {
            //双重检查
            synchronized (SingletonExample4.class) {//同步锁
                //还是空的话
                if (instance == null) {
                    instance = new SingletonExample4();
                }
            }
        }
        return instance;
    }
}

package com.huhao.concurrency.example.singleton;

import com.huhao.concurrency.annoations.ThreadSafe;

/**
 * 懒汉模式 - 双层同步锁单例模式
 * 单例的实例在第一次使用时候进行创建
 */
@ThreadSafe
public class SingletonExample5 {
    /**
     * 单例构造器
     * 必须私有,不能外部调用
     */
    private SingletonExample5() {
    }

    // 1、memory = allocate() 分配对象的内存空间
    // 2、ctorInstance() 初始化对象
    // 3、instance = memory设置ins tance指向刚分配的内存

    // instance添加volatile关键字,限制指令重排


    //单例对象
    private volatile static SingletonExample5 instance = null;

    //静态的工程方法
    public static SingletonExample5 getInstance() {
        if (instance == null) {
            //双重检查
            synchronized (SingletonExample5.class) {//同步锁
                //还是空的话
                if (instance == null) {
                    instance = new SingletonExample5();
                }
            }
        }
        return instance;
    }
}
package com.huhao.concurrency.example.singleton;

import com.huhao.concurrency.annoations.ThreadSafe;

/**
 * 饿汉模式
 * 单例的实例在类装载时候进行创建
 * <p>
 * but:构造器中如果处理的东西比较多,则该类会加载比较慢,
 * 产生性能问题
 */
@ThreadSafe
public class SingletonExample6 {
    /**
     * 单例构造器
     * 必须私有,不能外部调用
     */
    private SingletonExample6() {
    }

    //单例对象
    private static SingletonExample6 instance = null;

    static {
        instance = new SingletonExample6();
    }

    //静态的工程方法
    public static SingletonExample6 getInstance() {
        return instance;
    }


  /**
     * 静态代码块位置不同,结果也会不同
     * 是按顺序执行的
     *
     * @param args
     */
    public static void main(String[] args) {
        /**
         *     static {
         *         instance = new SingletonExample6();
         *     }
         *
         *    //单例对象
         *     private static SingletonExample6 instance = null;
         */
        System.out.println(getInstance());//null
        System.out.println(getInstance());//null

        /**
         *    //单例对象
         *     private static SingletonExample6 instance = null;
         *
         *     static {
         *         instance = new SingletonExample6();
         *     }
         */
        System.out.println(getInstance());//com.huhao.concurrency.example.singleton.SingletonExample6@3a71f4dd
        System.out.println(getInstance());//com.huhao.concurrency.example.singleton.SingletonExample6@3a71f4dd
    }
}
package com.huhao.concurrency.example.singleton;

import com.huhao.concurrency.annoations.Recommend;
import com.huhao.concurrency.annoations.ThreadSafe;

/**
 * 通过枚举类实现:最安全
 * 线程安全+推荐
 */
@ThreadSafe
@Recommend
public class SingletonExample7 {
    //私有构造函数
    private SingletonExample7() {
    }

    public static SingletonExample7 getInstance() {
        return Singleton.INSTANCE.getInstance();
    }

    private enum Singleton {
        INSTANCE;

        private SingletonExample7 singleton;

        //JVM保证这个方法绝对只调用一次
        Singleton() {
            singleton = new SingletonExample7();
        }

        public SingletonExample7 getInstance() {
            return singleton;
        }
    }
}

相关文章

网友评论

      本文标题:5、Java并发编程入门与高并发面试-安全发布对象

      本文链接:https://www.haomeiwen.com/subject/ncskahtx.html