This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

singleton class can be mutithreaded?

 
Ranch Hand
Posts: 588
  • 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?
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • 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: 588
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • 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.
 
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.
 
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.
 
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.
 
Ranch Hand
Posts: 225
Eclipse IDE Debian 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.
 
A timing clock, fuse wire, high explosives and a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!