Re: Race condition on singleton
Mark Thornton <mthornton <at> optrak.co.uk>
2011-01-06 08:26:29 GMT
On 06/01/2011 07:25, Mohan Radhakrishnan wrote:
> Hi,
> What about this ? Is this a similar singleton ? Since the map is static we haven't anticipated any problem.
>
> Should the method be synchronized ? There could be a problem with a partially created object ? Logically
even if threads get interleaved the map will anyway have the right object.
>
> We run this code with about 10-15 threads with no problem.
You got lucky, that code is badly broken. The simplest fix would be to
synchronize the getInstance method.
Mark Thornton
>
> private static Test test = null;
>
> private static Hashtable<String, Test> logMap = new Hashtable<String, Test>();
>
> public static Test getInstance( String logName ) {
>
> if (null == logMap.get(logName)) {
>
> test = new Test (logName);
>
> logMap.put(logName, test);
>
>
> } else {
>
> test = logMap.get(logName);
>
> }
>
> return test;
>
> }
>
> Thanks,
> Mohan
> -----Original Message-----
> From: concurrency-interest-bounces <at> cs.oswego.edu
[mailto:concurrency-interest-bounces <at> cs.oswego.edu] On Behalf Of Marco Villalobos
> Sent: Monday, December 13, 2010 10:58 AM
> To: concurrency-interest
> Subject: Re: [concurrency-interest] Race condition on singleton
>
> By simply reading his code, which I assume is an abridged version of
> the original, I didn't notice anything that would cause an NPE.
>
> However, if his original code uses static variables, then order of
> those declarations will matter.
>
> For example:
>
> public class WillNPE {
>
> private final static WillNPE instance = new WillNPE();
> private final static Set<Integer> set = new HashSet<Integer>();
>
> public static WillNPE getInstance() {
> return instance;
> }
>
> public WillNPE() {
> set.add(1);
> }
>
> public static void main(String args[]) {
> WillNPE local = WillNPE.getInstance();
> }
> }
>
>
> On Sun, Dec 12, 2010 at 9:10 PM, Justin T. Sampson<justin <at> krasama.com> wrote:
>> 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
>>>
>>
>> _______________________________________________
>> Concurrency-interest mailing list
>> Concurrency-interest <at> cs.oswego.edu
>> http://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
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest <at> cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest