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 {
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
source
share