Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

SPI实现的一点小疑问 #53

Open
ALL-AC opened this issue Sep 26, 2021 · 2 comments
Open

SPI实现的一点小疑问 #53

ALL-AC opened this issue Sep 26, 2021 · 2 comments

Comments

@ALL-AC
Copy link

ALL-AC commented Sep 26, 2021

image
我看这个 EXTENSION_INSTANCES 只是用来实例化,那么能不能改成这样?
难道只是因为 putIfAbsent 是原子性的?在进入方法之前,不也用 synchronized 加锁了?

    private T createExtension(String name) {
        // 从加载类缓存中获取 class 对象
        Class<?> clazz = getExtensionClasses().get(name);
        if (clazz == null) {
            throw new RuntimeException("No such extension of name " + name);
        }
        T instance = null;
        try {
            instance = (T) clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            log.error(e.getMessage());
        }
        return instance;
    }
@ALL-AC
Copy link
Author

ALL-AC commented Sep 26, 2021

而且dubbo里面也是这么写,只是多了一个摧毁的操作

@xiaoguyueyue
Copy link
Contributor

我感觉源码里面就是ioc思想,一个类创建一次就好了,可以一直用,你每次用的时候都创建一个新的,效率多低

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants