[concurrency-interest] Race condition on singleton

Paul McLachlan pmclachlan at gmail.com
Sun Dec 12 04:31:23 EST 2010


In my experience, problems like this are caused by circular references
between static initializers in different classes, causing one of the classes
to necessarily see the other in a half-constructed state.  If you haven't
already, I'd suggest carefully looking from that angle.

Regards,
Paul


On Sat, Dec 11, 2010 at 11:52 PM, Guy Korland <gkorland at gmail.com> wrote:

> David,
>
> We just noticed that the NPE also accord in external code to our code
> And again this NPE is on final field!
>
> java.lang.NullPointerException
>     at ch.qos.logback.classic.Logger.filterAndLog_1(Logger.java:426)
>     at ch.qos.logback.classic.Logger.info(Logger.java:627)
>
> Regards,
> Guy Korland
>
>
>
> On Fri, Dec 10, 2010 at 5:31 PM, Guy Korland <gkorland at gmail.com> wrote:
>
>> David,
>>
>> we got it while running jdk6u19.
>> The problem is that it happened to us only once and in two different
>> places in the code that uses the same pattern.
>>
>> Regards,
>> Guy Korland
>> -----Original Message-----
>> From: David Holmes
>> Sent:  10-12-2010, 00:31
>> To: Guy Korland; concurrency-interest
>> Subject: RE: [concurrency-interest] Race condition on singleton
>>
>>
>> Hi Guy,
>>
>> In addition to compilable/runnable test case can you tell us which
>> platform
>> and which JDK version please. Also can you test if it happens
>> with -Xint -client and -server.
>>
>> Other than the typo in the variable name there's nothing wrong with your
>> code and static initialization of the class should be ensuring full
>> visibility.
>>
>> David Holmes
>>  -----Original Message-----
>>  From: concurrency-interest-bounces at cs.oswego.edu
>> [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Guy
>> Korland
>>  Sent: Thursday, 9 December 2010 10:45 PM
>>  To: concurrency-interest
>>   Subject: [concurrency-interest] Race condition on singleton
>>
>>
>>  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/20101212/e5b2b5cc/attachment.html>


More information about the Concurrency-interest mailing list