[concurrency-interest] DCL clarification Immutable singelton

vikas vikas.vksingh at gmail.com
Thu Apr 10 10:03:50 EDT 2014


So it means the code below can be fixed by adding a Temporary variable.
So its now a classic case of benign data race. The below method will never
return null but may compute more than one Resource Objects

public class UnsafeLazyInitialization {
    private static Resource resource;

    public static Resource getInstance() {
        Resource temp = resource; // only one read of shared variable
        if (temp == null)
             resource = temp = new Resource();  // unsafe publication
        return temp; 
    }
}





--
View this message in context: http://jsr166-concurrency.10961.n7.nabble.com/DCL-clarification-Immutable-singelton-tp10862p10877.html
Sent from the JSR166 Concurrency mailing list archive at Nabble.com.


More information about the Concurrency-interest mailing list