This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

singleton class can be mutithreaded?

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
singleton means one instance in the entire application,so a singleton class can be multithreaded?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • 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: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Croarkin
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • 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.
 
Vinod K Singh
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • 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.
 
Amit Siinngghh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic