• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic