Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes singleton class can be mutithreaded? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "singleton class can be mutithreaded?" Watch "singleton class can be mutithreaded?" New topic
Author

singleton class can be mutithreaded?

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
singleton means one instance in the entire application,so a singleton class can be multithreaded?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Apr 02, 2008
Posts: 576
Thank you
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

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

Joined: Sep 30, 2004
Posts: 106
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.


Thanks,<br /> <br />Paul Croarkin<br />SCEA 5, SCWCD, SCJP
Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198
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.


My Blog
Amit Siinngghh
Greenhorn

Joined: Jun 19, 2004
Posts: 12
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

Joined: May 27, 2008
Posts: 225

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: singleton class can be mutithreaded?