Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
    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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

why use singleton to ensure thread safety

Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Report post to moderator
I've got two strikes against me. First I can't get my head around threads. Second, although I understand what the singleton pattern is, I don't understand why you would want to use it to protect memory. Everytime I ask someone why the singleton pattern is used they tell me that it is to protect memory and they wave their hands around in the air. Is this a big hoax perpetuated by someone who gets a commission everytime the singleton pattern is used? Or is there a solid explanation as to how the singleton pattern protects memory?

In my mind, it seems like it should be the other way around. That the singleton pattern causes MORE problems because the same instance variables are being by multiple threads.

Here is the way that I look at the situation--threads are bratty little children and objects are shiny red fire trucks. Instance variables are the little plastic men that ride on the fire trucks. A class variable is the single firehose that exists to hook up to all the firetrucks. In a singleton class fire truck all the bratty children are playing with the same toy. There is one plastic man and one firehose. Wouldn't it be better if this was not a singleton class and every child had their own fire truck with its own little plastic man so that they didn't interfere with each other?

Please help me to clarify my understanding.
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
  • Report post to moderator
I'm going to move this to Java In General (JIG) Intermediate.
It has more to do with the core Java language features and OOP than with Servlets.
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Report post to moderator
Many people think Singletons are over-used. Some people think they should never be used. Where it is possible for each thread to have its own instance of a particular class, it is very often good for that to be done.

But sometimes a singleton does form a convenient meeting point for various threads and/or objects to co-ordinate their access to some aspect of the system of which there should only ever be one.

I'm not sure what your colleagues mean by "singletons are to protect memory". Singletons can be used to co-ordinate various threads' access to a particular bit of memory, if there's some reason why you need to do that.

Single-threaded programs can use singletons. Many do, sometimes wisely, sometimes not. Where there are multiple threads possibly accessing a singleton, the singleton class must be coded with appropriate synchronisation.

Of course, singletons in Java are one-per-ClassLoader, not one-per-JVM. Sometimes this can trip you up. Another reason not to use singletons, some might say.
author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Report post to moderator
This is a crosspost of this. I'm going to close this one. Please don't crosspost.
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic