为什么在 Struts 1.2.7 中延迟实例化 MessageResourcesFactory?

发布于 2024-07-29 03:20:08 字数 1218 浏览 3 评论 0原文

由于存在双重检查锁定问题,因此我们必须使用同步来保证对以下方法(org.apache.struts.util.MessageResources 类)的并发访问:

INSTANTIATION

public synchronized static MessageResources getMessageResources(String config) {

    if (defaultFactory == null) {
        defaultFactory = MessageResourcesFactory.createFactory();
    }

    return defaultFactory.createResources(config);
}

为什么不使用:

EAGER INSTANTIATION

static {

    // Construct a new instance of the specified factory class
    try {
        if (clazz == null)
            clazz = RequestUtils.applicationClass(factoryClass);
        MessageResourcesFactory defaultFactory =
            (MessageResourcesFactory) clazz.newInstance();
    } catch (Exception e) {
        LOG.error("MessageResourcesFactory.createFactory", e);
    }

}

然后:

public static MessageResources getMessageResources(String config) {

    return defaultFactory.createResources(config);
}

LAZY 将允许并发访问 getMessageResources 方法,至少在我的情况下,它可能会被调用很多次。

不使用同步时的含义如下:

http://en.wikipedia.org/wiki/双重检查锁定

Since there is the Double-checked locking issue so we have to use synchronization to guarantee the concurrent access to the following method (org.apache.struts.util.MessageResources class) :

LAZY INSTANTIATION

public synchronized static MessageResources getMessageResources(String config) {

    if (defaultFactory == null) {
        defaultFactory = MessageResourcesFactory.createFactory();
    }

    return defaultFactory.createResources(config);
}

Why not to use:

EAGER INSTANTIATION

static {

    // Construct a new instance of the specified factory class
    try {
        if (clazz == null)
            clazz = RequestUtils.applicationClass(factoryClass);
        MessageResourcesFactory defaultFactory =
            (MessageResourcesFactory) clazz.newInstance();
    } catch (Exception e) {
        LOG.error("MessageResourcesFactory.createFactory", e);
    }

}

And then:

public static MessageResources getMessageResources(String config) {

    return defaultFactory.createResources(config);
}

It would allow concurrent access to the method getMessageResources which at least in my case it may be called quite a few times.

The implications when not using synchronized are in here:

http://en.wikipedia.org/wiki/Double-checked_locking

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初熏 2024-08-05 03:20:08

MessageResourcesFactory 是线程安全的吗? synchronized 方法保护字段的设置和 createResources 方法调用。 如果它是线程安全的,则可以将锁定移动到仅设置字段并将方法调用保留在临界区之外。

Is MessageResourcesFactory thread-safe? The synchronized method protects both the setting of the field and the createResources method call. If it is thread-safe the locking could be moved to cover just setting the field and leave the method call outside the critical section.

只怪假的太真实 2024-08-05 03:20:08

现代 JVM 上同步方法产生的开销非常小,可以忽略不计。 对同步的lazy-init工厂方法的后续调用将与调用非同步的eager-init方法一样快。

就代码而言,惰性初始化方法比使用静态初始化块更简单、更容易理解(在我看来)。 此外,当静态初始化块失败时,找出位置和原因可能会非常令人困惑。

The overhead incurred by synchronized methods on a modern JVM is so small as to become insignificant. Subsequent calls to the synchronized lazy-init factory method will be pretty much as fast as would calls to an unsynchronized eager-init method.

In terms of code, the lazy-init approach is simpler and easier to understand (in my opinion) than using a static initializer block. Also, when static init blocks fail, it can be very confusing to figure out where and why.

花桑 2024-08-05 03:20:08

除非有任何原因 MessageResourceFactory 无法提前初始化(例如,某些 Servlet 资源需要首先初始化),否则我想我更喜欢您的解决方案。 我猜想,Struts 团队没有理由延迟加载工厂,除非这是从事此工作的特定开发人员习惯做的事情(人们确实倾向于延迟加载单例,即使没有必要)。

您是否尝试过提交错误报告并提出解决方案?

Unless there's any reason the MessageResourceFactory cannot be initialized early on (e.g., certain Servlet resources need to be initialized first), I think I like your solution better. I would guess that there's no reason for the Struts team to lazily load the factory other than that that's what the particular developer who worked on this is used to doing (people do tend to lazily load singletons even when it's not necessary).

Have you tried submitting a bug report, and proposing your solution?

别再吹冷风 2024-08-05 03:20:08

我认为这是 Struts 确保它在多线程模式下正常工作的一种方法,无论重写 org.apache.struts.util.MessageResources 的人是否将 createResources(String configuration) 定义为同步或不同步。

I think it is a way for Struts to make sure that it works fine when in multi-thread mode, no matter if the person overriding org.apache.struts.util.MessageResources defines createResources(String configuration) as synchronized or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文