This week's giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of liveProject: Build an ML Recommender System and have Kim Falk on-line!
See this thread for details.
Win a copy of liveProject: Build an ML Recommender System this week in the Artificial Intelligence and Machine Learning 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Server Side Singletons

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the benefit to using Singletons on the server? I was reading another post and somebody had multiple calling classes as Singletons. While it reduces object creation, wouldn't you have to synchronize a lot of the code, creating a whole bunch of synchronized bottle necks?

I can justify having the class that accesses the database file a singleton since it's already all synced up, but what about any front end facade/adapter classes?
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wouldn't you have to synchronize a lot of the code, creating a whole bunch of synchronized bottle necks?



Not necessarily. You can also make Singletons that are completely threadsafe and totally unsynchronized. As long the Singleton only accesses its own ***unchanging*** state or local data from the method calls it is perfectly threadsafe. If you need to maintain client-state between method calls then you must you use some sort of mechanism to hold the state other than instance or static fields on your Singleton. A common approach used by many frameworks is to use a ThreadLocal to store the client data (see the Javadoc for more info).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic