Hi,

What about the singleton implementation using java enum? enum provides
inbuilt thread safety. But still it works only for jdk 6 or later versions.

On Fri, Dec 19, 2014 at 6:09 PM, Udara Liyanage <[email protected]> wrote:
>
> Hi,
>
> Several places have several methods. However I like the way of having an
> inner static class. Java make sure a class is load only once, thus only one
> object is created.
>
>
>    1. class Singleton {
>    2. private static class InstanceHolder {
>    3. public static Singleton instance = new Singleton();
>    4. }
>    5.
>    6. public static Singleton getInstance() {
>    7. return InstanceHolder.instance;
>    8. }
>    9. }
>
>
> On Fri, Dec 19, 2014 at 3:19 PM, Manuranga Perera <[email protected]> wrote:
>>
>>
>> ---------- Forwarded message ----------
>> From: Manuranga Perera <[email protected]>
>> Date: Fri, Dec 19, 2014 at 3:15 PM
>> Subject: What is the recommended way to initialize singleton
>> To: dev <[email protected]>
>> Cc: Afkham Azeez <[email protected]>, Sameera Jayasoma <[email protected]>,
>> Sameera Perera <[email protected]>, Ruchira Wageesha <[email protected]>
>>
>> Following kind of double locking code doesn't work [1] for singletons.
>> (please remove this if you see it in any place in the platform)
>>
>> if(instance == null) {
>>     synchronized(MyClass.class) {
>>         if(instance == null) {
>>             instance = new MyClass();
>>         }
>>     }
>> }
>> return instance;
>>
>>
>> There are multiple ways we can do this right. What is the best practice
>> for defining singletons in unavoidable situations?
>>
>> [1]
>> http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
>>
>> --
>> With regards,
>> *Manu*ranga Perera.
>>
>> phone : 071 7 70 20 50
>> mail : [email protected]
>>
>>
>> --
>> With regards,
>> *Manu*ranga Perera.
>>
>> phone : 071 7 70 20 50
>> mail : [email protected]
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "WSO2 Engineering Group" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/a/wso2.com/d/optout.
>>
>
>
> --
>
> Udara Liyanage
> Software Engineer
> WSO2, Inc.: http://wso2.com
> lean. enterprise. middleware
>
> web: http://udaraliyanage.wordpress.com
> phone: +94 71 443 6897
>
> --
> You received this message because you are subscribed to the Google Groups
> "WSO2 Engineering Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/a/wso2.com/d/optout.
>


-- 
Gimantha Bandara
Software Engineer
WSO2. Inc : http://wso2.com
Mobile : +94714961919
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to