On 05/11/2012 09:51 AM, Adam Heath wrote:
> On 05/11/2012 09:36 AM, Jacopo Cappellato wrote:
>>
>> On May 11, 2012, at 4:26 PM, Adam Heath wrote:
>>
>>>>>>> @@ -177,10 +184,18 @@ public class GroovyUtil {
>>>>>>> Class<?> scriptClass = parsedScripts.get(script);
>>>>>>> if (scriptClass == null) {
>>>>>>> scriptClass = loadClass(script);
>>>>>>> - if (Debug.verboseOn()) Debug.logVerbose("Caching Groovy script: "
>>>>>>> + script, module);
>>>>>>> - parsedScripts.put(script, scriptClass);
>>>>>>> + synchronized (parsedScripts) {
>>>>>>> + Class<?> scriptClassCached = parsedScripts.get(script);
>>>>>>> + if (scriptClassCached == null) {
>>>>>>> + if (Debug.verboseOn()) {
>>>>>>> + Debug.logVerbose("Caching Groovy script: " + script, module);
>>>>>>> + }
>>>>>>> + parsedScripts.put(script, scriptClass);
>>>>>>> + } else {
>>>>>>> + scriptClass = scriptClassCached;
>>>>>>> + }
>>>>>>> + }
>>>>>>> }
>>>>>>> -
>>>
>>> This is DCL.
>>
>> Oh no, you are wrong Adam: it would be DCL if scriptClass was a shared
>> variable, which is not: it is a *local* variable.
And of course scriptClass needs to locking, because of *course* it is
local. But parsedScripts is shared, needs protection, but the pattern
you introduced is the DCL. DCL is about protecting shared resources.
> ==
> result = parsedScripts.get(key)
> if (result == null) {
> syncyronized (parsedScripts) {
> result = parsedScripts.get(key)
> if (result == null) {
> result = genResult()
> parsedScripts.put(key, result)
> }
> }
> }
> return result
> ==
>
> DCL is about the protected resource, parsedScripts. Please try again.
>
> The clue here it to look at what is being protected by the
> synchronized keyword.