Hi,
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 (Throwable t) {
LOG.error("MessageResourcesFactory.createFactory", t);
}
}
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: