• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple Singleton instances.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to have multiple singleton instances if classes loaded by different classloaders access a singleton.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic