This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Multiple Singleton instances. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multiple Singleton instances." Watch "Multiple Singleton instances." New topic
Author

Multiple Singleton instances.

chhota don
Greenhorn

Joined: Mar 03, 2004
Posts: 6
Is it possible to have multiple singleton instances if classes loaded by different classloaders access a singleton.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Chhota,
Interesting question. According to me, there will be two instances of a singleton in one JVM if the singleton class was loaded more than once by different classloaders. Now, here is the catch - "only if the SINGLETON CLASS is loaded by different class loaders"...
First I would explain my theory about why there would be two instances of singleton if the singleton class is loaded by two different classloaders and then I would explain in detail about the "catch" I mentioned above.
Part-1]
--------
Here is my theory for first part,
- Singleton has a static method to obtain an object ref
- Static methods are at the class level
- Hence if a class is twice there in memory we have two copies of those static methods and the static private variable (as per the singleton impl)
Thus when we call classname.getInstance(), it will check in corresponding private variable to see if its null or not (according to the singleton logic) so essentially it will allow us to create two instances of the object in memory...
Part-2] What about the "catch"?
--------------------------------
NOW, as in your question, if the class loaded by different class loaders refers to singleton what happens? Here, we have to see if the singleton class is also loaded by the corresponding class loaders or not.
For e.g. we have a singleton class S and a class X using S.
AND Class X is loaded twice by two different classloaders - CL1, CL2.
If S is always loaded by some other class loader CL3 (this might happen due to some hierarchy of classloaders etc you know) then it would mean that- no matter that X is loaded twice (once by CL1 and then CL2), S would be always loaded once (that is by CL3) AND hence there would be only one instance of singelton ever created.
But if the S is also loaded by CL1 or CL2 correspondingly then, I would say that "there would be two instances of S" (one from CL1, another from CL2).
Hopefully I am able to express myself.
Regards
Maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
sorry,
just a correction. On the first line where I say,

there will be two instances of..

I mean "there will be more than one instances of..."
Thanks
Maulin
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Since 'Singleton' is a design-pattern, and not part of the java-language,

it depends, how the singleton is implemented.
If someone wants his Singleton to be single over multiple JVMs or Classloaders, he could have used sockets to guarantee singleton-ness.
If you wan't multiple Singletons, the name 'Singleton' gets a bit foggy.


http://home.arcor.de/hirnstrom/bewerbung
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Stephan
Agree w/ you but here it seems we already have singleton implemented the standard way and the question arises...I am not sure about scenarios that requires Singleton across JVMs ....may be in some UUID generation programs or something...
Also, in my theory that I mentioned above, I am not sure that in single JVM whether it is possible to have class S loaded by two different class loaders? This is because if Class X refers to S and even if X is loaded by two different class loaders CL1,CL2, class S would be somewhere in classloader hierarchy so that it is visible to CL1 and CL2 both (otherwise it would throw ClassNotFoundException ,right?) and hence its loaded by a third class loader CL3...hence ONLY ONE INSTANCE PER JVM for the singleton class...
Regards
Maulin
Aneesha Singh
Ranch Hand

Joined: Jan 14, 2002
Posts: 47
Hi guys,
I found an article on Javaworld that may be interesting to you, its about Singletons,
http://www.javaworld.com/javaworld/jw-01-2001/jw-0112-singleton.html
I think it is very relevant to this discussion!
Cheers!
Aneesha Singh
Ranch Hand

Joined: Jan 14, 2002
Posts: 47
Hi guys,
I found an article on Javaworld that may be interesting to you, its about Singletons,
http://www.javaworld.com/javaworld/jw-01-2001/jw-0112-singleton.html
I think it is very relevant to this discussion!
Cheers!
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by chhota don:
Is it possible to have multiple singleton instances if classes loaded by different classloaders access a singleton.

Yes. There is nothing which can overcome this. If its a problem you must design around it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Multiple Singleton instances.