[concurrency-interest] Race condition on singleton

Tim Halloran hallorant at gmail.com
Thu Dec 9 09:44:49 EST 2010


On Thu, Dec 9, 2010 at 8:39 AM, Rémi Forax <forax at univ-mlv.fr> wrote:

>  Hi guy,
> do you have a small Java file that reproduce the bug ?
>

Yes that would be useful, the declaration of the "MyClass" field

private static final MyClass = new MyClass();

appears to be inconsistent with its use as "myClass"

return myClass;

Tim

>
> Rémi
>
>
>
> On 12/09/2010 01:44 PM, Guy Korland wrote:
>
>  Hi,
>
>  We found a very strange pattern that seems like contradicting the Java
> Memory Model.
> It seems like a class that is maintained as singleton doesn't have its
> constructor fully initialized!
> See the code example bellow.
>
>  public class MyClass{
>
>    private static final MyClass = new MyClass();
>
>   private final HashMap map;
>
>    private MyClass(){
>       map = new HashMap();
>   }
>
>    public void put(Object k, Object v){
>      map.put(k,v);
>   }
>
>    static public getMyClass(){
>     return myClass;
>   }
> }
>
>
>  And when we invoke the following:
>
>  MyClass.getMyClass().put("a","b");
>
>  We get a NullPointerException on the "map.put(k,v);", meaning the
> map==null !?!?
>
>  Any ideas?
>
> Thanks,
> Guy Korland
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.eduhttp://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
>
> _______________________________________________
> 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/20101209/305843e5/attachment.html>


More information about the Concurrency-interest mailing list