• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is Singleton needed for thread safety  RSS feed

 
Robin Clark
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For years I have not understood why the singleton design pattern is used. I understand what the singleton pattern is, I just don't understand "why". Can someone direct me towards some pictures that depict what happens to static data members and non-static data members in a multi-threaded process that would cause problems for a class that was not a singleton? And then a picture of what happens to fix the problem in a singleton class? I've read many descriptions over the years but still can not visualize what occurs in memory for:

threads
singleton versus non-singleton
static data members
non static data members

Please keep the explanation as simple as possible because I must be extremely dim-witted about this concept!

Thank you!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing about Singleton that necessarily helps with thread safety, certainly at least not compared to a pile of static methods and variables. The most frequent discussion of Threads and Singletons together that you'll hear is how to implement Singleton in the presence of multiple threads, because it can be tricky.

The reasons for using Singleton generally have nothing to do with multithreading.
 
Robin Clark
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the reasons that I would use a singleton. I would use a singleton if I wanted to share resources. Everyone could instantiate the object and certain activities would only have to occur once like getting a database connection.

But if I didn't want to share resources, I would absolutely think that the singleton pattern would be pointless.

Am I confused about why to use the singleton pattern? What are the benefits to the singleton pattern if not to share resources?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, please don't post the same question to multiple forums -- see here, please, for an explanation.

Anyway, regarding your question: using a Singleton is, indeed, one way to share resources or coordinate access to them, although it's not the only way or even the best way. Ultimately, I suppose, all uses of Singleton will boil down to something like that.

One of the few truly valid uses for Singleton is simply to avoid creating redundant objects. For example, if you define a class (for example, a class that implements java.util.Comparator) with no member data at all, every instance is equivalent, so it's a waste (albeit a small waste!) to allocate more than one. Therefore, you might use the Singleton pattern to share one instance across all uses of the class.

Note that in this example, static methods won't help you -- you need to implement the interface with a non-static method. Using the Singleton pattern is the only way to avoid creating redundant objects.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!