Coder Social home page Coder Social logo

Comments (5)

1228857713 avatar 1228857713 commented on June 3, 2024

image

标准的dcl 会添加 volatile 关键词来防止 指令重排, 这里 instance 指令增加了volatile 可以防止以下两个指令重排序的问题导致其他线程拿到了 instance 对象,而该对象确没有完成实例化

image

from guide-rpc-framework.

1228857713 avatar 1228857713 commented on June 3, 2024

不知道在上述情况下是否需要 添加 volatile 来防止上述问题
image

请大神解答一下我的疑惑

from guide-rpc-framework.

javaguide-tech avatar javaguide-tech commented on June 3, 2024

https://github.com/Snailclimb/guide-rpc-framework/blob/master/rpc-framework-common/src/main/java/github/javaguide/factory/SingletonFactory.java

public static <T> T getInstance(Class<T> c) {
        String key = c.toString();
        Object instance = null;
        if (instance == null) {
            synchronized (SingletonFactory.class) {
                instance = OBJECT_MAP.get(key);
                if (instance == null) {
                    try {
                        instance = c.getDeclaredConstructor().newInstance();
                        OBJECT_MAP.put(key, instance);
                    } catch (IllegalAccessException | InstantiationException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    } catch (NoSuchMethodException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return c.cast(instance);
    }

这一段感觉在模仿标准的 dcl 写法,但是

Object instance = null;
if (instance == null) {
synchronized (SingletonFactory.class) {

明显 instance 肯定为空,判断多余,且synchronized (SingletonFactory.class) { 使用 类锁,锁的力度很大,在多线程情况下并发度很低阻塞会很严重

这里直接把最外层判断 instance 对象是否为空给移除就可以了吧!

from guide-rpc-framework.

1228857713 avatar 1228857713 commented on June 3, 2024

https://github.com/Snailclimb/guide-rpc-framework/blob/master/rpc-framework-common/src/main/java/github/javaguide/factory/SingletonFactory.java

public static <T> T getInstance(Class<T> c) {
        String key = c.toString();
        Object instance = null;
        if (instance == null) {
            synchronized (SingletonFactory.class) {
                instance = OBJECT_MAP.get(key);
                if (instance == null) {
                    try {
                        instance = c.getDeclaredConstructor().newInstance();
                        OBJECT_MAP.put(key, instance);
                    } catch (IllegalAccessException | InstantiationException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    } catch (NoSuchMethodException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return c.cast(instance);
    }

这一段感觉在模仿标准的 dcl 写法,但是

Object instance = null;
if (instance == null) {
synchronized (SingletonFactory.class) {

明显 instance 肯定为空,判断多余,且synchronized (SingletonFactory.class) { 使用 类锁,锁的力度很大,在多线程情况下并发度很低阻塞会很严重

这里直接把最外层判断 instance 对象是否为空给移除就可以了吧!

这不行把,那所有的对象获取,都会进入锁竞争嘛。那效率就会很低

from guide-rpc-framework.

Snailclimb avatar Snailclimb commented on June 3, 2024

https://github.com/Snailclimb/guide-rpc-framework/blob/master/rpc-framework-common/src/main/java/github/javaguide/factory/SingletonFactory.java

public static <T> T getInstance(Class<T> c) {
        String key = c.toString();
        Object instance = null;
        if (instance == null) {
            synchronized (SingletonFactory.class) {
                instance = OBJECT_MAP.get(key);
                if (instance == null) {
                    try {
                        instance = c.getDeclaredConstructor().newInstance();
                        OBJECT_MAP.put(key, instance);
                    } catch (IllegalAccessException | InstantiationException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    } catch (NoSuchMethodException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return c.cast(instance);
    }

这一段感觉在模仿标准的 dcl 写法,但是

Object instance = null;
if (instance == null) {
synchronized (SingletonFactory.class) {

明显 instance 肯定为空,判断多余,且synchronized (SingletonFactory.class) { 使用 类锁,锁的力度很大,在多线程情况下并发度很低阻塞会很严重

这里直接把最外层判断 instance 对象是否为空给移除就可以了吧!

这不行把,那所有的对象获取,都会进入锁竞争嘛。那效率就会很低

已经确认您的PR了 老哥

from guide-rpc-framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.