Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronizing class instantiation  RSS feed

 
David Irwin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm using Java reflection to instantiate a class but am running into a rather strange problem. In particular, even though the block of code that instantiates the object is synchronized, it appears that I sometimes get back the same exact object instead of a new object. For example, here's the code block that creates a new object:



Yet, on maybe 20% of the time the hardware driver object returned is identical to the previous hardware driver class.

(What do I mean by identical? For my testing, by identical I really mean this: the driver class contains a random number generator and a creation-time field equalling System.currentTimeMillis(). Two drivers are equal if the random number generator and creation time fields are equal.)

I'm really at a loss how the above block could be returning identical class objects? Any ideas?

Thanks,
Dave
[ June 19, 2006: Message edited by: David Irwin ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this is anything to do with synchronisation.

There is no way that your code can return identical object references. Every call will instantiate a new object. You do not need any synchronisation to ensure that. In fact, the synchronisation that you have is unnecessary, unless any of the method calls (to code you haven't posted) require it.

However, you need to be careful with your terminology. In Java, "identical" object references point to the exact same object, whereas "equal" objects can be separate objects that are considered equal according to the equals() method.

It may be possible for your method to return equal objects, depending on the semantics of equals() for your objects. By default, equals() tests for identity, so the default equals() can never return true for your code. So, I guess equals() has been overridden for your class; you should go look in detail at that.
[ June 20, 2006: Message edited by: Peter Chase ]
 
David Irwin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
I don't think this is anything to do with synchronisation.

There is no way that your code can return identical object references. Every call will instantiate a new object. You do not need any synchronisation to ensure that. In fact, the synchronisation that you have is unnecessary, unless any of the method calls (to code you haven't posted) require it.

[ June 20, 2006: Message edited by: Peter Chase ]


I went back and found that indeed this method was creating different object references (had different memory addresses). Somehow, however, the instantiation of the object was getting the same reference to a random number generator (private final RNG rng = new RNG(); in the class variable declaration). To solve the problem, I made the RNG a static final object (the RNG class is thread safe). Haven't had any issues since.

Thanks,
Dave
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!