On 05/11/2012 10:30 AM, Jacopo Cappellato wrote:
> On May 11, 2012, at 4:51 PM, Adam Heath wrote:
> 
>> ==
>> 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.
> 
> Just to be sure I understand: are you saying that the above code is an 
> example of the DCL anti-pattern and it is wrong?

I'm not saying that.  Others are saying that, and I'm just repeating it.

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

==
class Foo {
    private Helper helper = null;
    public Helper getHelper() {
        if (helper == null) {
            synchronized(this) {
                if (helper == null) {
                    helper = new Helper();
                }
            }
        }
        return helper;
    }

    // other functions and members...
}
==

In my example, the protected variable is parsedScripts(instead of
this), the get/put on the map is setting the variable on the class.

Reply via email to