[concurrency-interest] DCL clarification Immutable singelton

Vitaly Davidovich vitalyd at gmail.com
Thu Apr 10 10:47:32 EDT 2014


No, it cannot return null.  What you have there is basically a benign data
race-like construct except you ensure only one instance is ever created and
that same instance is returned.

I do agree with Hans and Aleksey that you really should try to avoid
constructs like this in real code as it's quite brittle.

Sent from my phone
On Apr 10, 2014 10:17 AM, "vikas" <vikas.vksingh at gmail.com> wrote:

> But is the same benign data race thing can be applied to DCL.
> i.e is the code below DCL with a temp variable and immutable Singleton, can
> ever
> return null? Is there a possible reordering which will allow the below code
> to return null.
>
> final class  Singleton {
>
>           private static  Singleton _instance;  // not a volatile variable
>
>           public static Singleton getInstance() {
>                             Singeltom temp = _instance;
>                             if(temp == null) {
>                                        synchronized(Singleton.class) {
>                                                  temp = _instance;
>                                                  if(temp == null) {
>                                                        _instance =temp =
> new
> Singleton();
>                                                  }
>                                        }
>                              }
>                             return temp;
>            }
>
>           private Singleton() {
>                       i=9;
>                       j=99;
>                       k=999;
>           }
>           *final* private int i,j,k;
> }
>
>
>
> --
> View this message in context:
> http://jsr166-concurrency.10961.n7.nabble.com/DCL-clarification-Immutable-singelton-tp10862p10878.html
> Sent from the JSR166 Concurrency mailing list archive at Nabble.com.
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140410/07ee41f2/attachment-0001.html>


More information about the Concurrency-interest mailing list