Easy peasy, Make it thread safe by adding the synchronized keyword.

     public static *synchronized* C getInstance() throws YourException { 
            if (mInstance == null )  { 
                  mInstance = new C(); //it throws YourException 
             }  

On Friday, May 4, 2012 10:17:37 AM UTC-4, Daniel Drozdzewski wrote:
>
> On 4 May 2012 14:45, Adriano B. Godinho <[email protected]> wrote: 
> > You can instantiate your singleton instance in a static method: 
> > 
> > class C{ 
> > 
> >     private static C mInstance = null; 
> > 
> >     public static C getInstance() throws YourException { 
> >            if (mInstance == null )  { 
> >                  mInstance = new C(); //it throws YourException 
> >             } 
> > 
> >            return mInstance; 
> >     } 
> > 
> > } 
>
>
> ...which changes the original code from eager initialisation to lazy 
> initialisation (not a bad thing per se and optimisation is some 
> circumstances), but it remains not thread safe. 
>
>
> -- 
> Daniel Drozdzewski 
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to