[concurrency-interest] DCL clarification Immutable singelton (vikas)

Valentin Kovalenko valentin.male.kovalenko at gmail.com
Wed Apr 9 05:53:16 EDT 2014


Yes, if i, j, k would be final your Singletor would be technically correct.
But I suppose invocations of getInstance() method MAY see _instance == null
more often than if field _instance would be volatile, and therefore
synchronized(...) block MAY be executed more often. Also I see no sense in
making your Singleton lazy, so you can just write
private static Singleton _instance = new Singleton();

code from the original question:

> final class  Singleton {
>           private static  Singleton _instance;
>           public static Singleton getInstance() {
>                             if(_instance == null) {
>                                        synchronized(Singleton.class) {
>                                                  if(_instance == null) {
>                                                        _instance = new
> Singleton();
>                                                  }
>                                        }
>                              }
>                             return _instance;
>            }
>           private Singleton() {
>                       i=9;
>                       j=99;
>                       k=999;
>           }
>           private int i,j,k;
> }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140409/fec1a0d0/attachment.html>


More information about the Concurrency-interest mailing list