[concurrency-interest] Race condition on singleton

Guy Korland gkorland at gmail.com
Mon Dec 13 00:47:12 EST 2010


The NPE for sure is not a problem of unsynchronized map.
I guess the "psdo code" I wrote to mimic the problem creates more issues
than help :(.

Anyway the problem is with the pattern and not with the Map since as I wrote
we saw it on the saw time in three different places (without maps).

Regards,
Guy Korland


On Mon, Dec 13, 2010 at 7:18 AM, David Holmes <davidcholmes at aapt.net.au>wrote:

>  I'm assuming from the way Guy stated it that it was the map.put that
> caused the NPE, not some internal action within the put() code.
>
> Of course stacktraces would make it clearer.
>
> David Holmes
>
> -----Original Message-----
> *From:* concurrency-interest-bounces at cs.oswego.edu [mailto:
> concurrency-interest-bounces at cs.oswego.edu]*On Behalf Of *Justin T.
> Sampson
> *Sent:* Monday, 13 December 2010 3:10 PM
> *To:* Guy Korland
> *Cc:* concurrency-interest
> *Subject:* Re: [concurrency-interest] Race condition on singleton
>
> I'm surprised with all the replies that no one commented on the lack of
> synchronization on accessing the contents of the map. That can easily cause
> NPEs from within map.put(k, v), or almost any other arbitrary behavior for
> that matter.
>
> Cheers,
> Justin
>
> On Thu, Dec 9, 2010 at 4:44 AM, Guy Korland <gkorland at gmail.com> 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.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/20101213/100abddb/attachment-0001.html>


More information about the Concurrency-interest mailing list