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?
posted 8 years ago
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).
kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
get schwifty. tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user