Why is lazy creating a MessageResourcesFactory in Struts 1.2.7?

Since there is a problem with Double-Check locking, we should use synchronization to guarantee concurrent access to the following method (class org.apache.struts.util.MessageResources):

LAZ INSTANCE

public synchronized static MessageResources getMessageResources(String config) {

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

    return defaultFactory.createResources(config);
}

Why not use:

INDEX EAGER

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);
}

This will allow simultaneous access to the getMessageResources method, which, at least in my case, can be called quite a few times.

In this case, the consequences of unsynchronization are implied:

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

+3
source share
4 answers

, Struts , , , org.apache.struts.util.MessageResources createResources (String configuration) .

0

MessageResourcesFactory ? synchronized , createResources. , , .

+1

, JVM, , . lazy-init factory , eager-init.

( ), . , init , , .

0

, MessageResourceFactory (, ), , . , , Struts factory, , , , , ( , ).

?

0
source

Source: https://habr.com/ru/post/1713629/


All Articles