• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

singleton class can be mutithreaded?

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
singleton means one instance in the entire application,so a singleton class can be multithreaded?
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jacob,

even a singleton object may be accessed simultaneously by multiple threads. You still have to take care of correct synchronization if you want to make access to it thread-safe. And there are subtle difficulties to create a real singleton in a multi-threaded application. You can't just use the typical singleton pattern if you want to guarantee that there will be really only one instance of this class. You'll find lots of discussions on this on the internet. There's a typical pattern called double-checked locking which still leaves the possibility that more than one instance of a class will be created. You'll find a correct solution in Brian Goetz's book but it's too complicated to explain all this here in short.

Marco
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jacob deiter:
singleton means one instance in the entire application,so a singleton class can be multithreaded?



Actually, this is not true. Google for "singleton considered stupid" for details. The Singleton pattern was made famous by the gang of four book, but it not nearly as simple, or as useful, as they indicate.

The problem is your phrase "entire application". In complex webservices, there may be more than one class loader, and this can cause there to be more than one loaded instance of your "singleton" which really means its not a singleton.

It is also a serious restriction on later "re-use" of your code if you don't make it thread safe. As any code that uses your code will become unsafe if your code is unsafe.

Today, 4 processor CPUs are cheap. Soon there will be 16 and 32 processor cheap systems.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As pointed out above, you need to know what is really meant by "single per application" due to multiple JVMs, etc

However, given one JVM, there is a simple way to safely construct only one copy of the object.

- make the constructor private
- instantiate the object at class load time



Note: this only ensures that one copy is created in a particular JVM. There can be other concurrency issues associated with publishing the object.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Note: this only ensures that one copy is created in a particular JVM. There can be other concurrency issues associated with publishing the object.


Singleton means one instance per classloader. As there can be multiple classloaders in one JVM, so it is possible to have multiple instances of a singleton class.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it more appropriate to say one Instance per "Class Loader Tree", I believe there is a classloader tree hierarchy (bootstrap, system, http, custom, etc) in a JVM.
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's mostly true, Amit. Some custom classloaders (like in Tomcat) may load code from their own classpath before their parents', and OSGI doesn't really arrange its custom classloaders in a tree at all.

It is true for most desktop and JEE applications, though.
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic