Meaningless Drivel is fun!*
The moose likes OO, Patterns, UML and Refactoring and the fly likes A Performance doubt on Singleton Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "A Performance doubt on Singleton Pattern" Watch "A Performance doubt on Singleton Pattern" New topic
Author

A Performance doubt on Singleton Pattern

Sandeep Lodhia
Ranch Hand

Joined: Oct 09, 2000
Posts: 105
Hi,
I know when to use Singleton Pattern.
But I am not sure of how multiple,simultaneous requests are handled for singleton class.I have following 2 questions :
1. There is no synchronized code in my Singleton class.And suppose 1 request is accessing this class.At the same time,another request for this class come.Is the 2nd request queued till 1st request is over?
2.I believe that simultaneous request can access singleton class simultaneously.Hence in any application, all classes like service classes, DAO classes, except EJBs, should be singleton. This will improve memory perfomance as number of objects are significantly less.
Pls correct me if I am wrong.
Thanks.
Sandeep.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Singleton is a specific pattern, and should really only be used in cases where there can only ever be one instance of a class (*). Correct use of the Singleton pattern ensures that there is just one object of the class in question, used by all client code of that class.
If you wish your code to be safe in multi-threaded situations, you should either only use variables local to methods, or ensure all access to shared data is "synchronized". Singleton objects are not immune to this rule.
As for converting all your classes to singletons, I don't see the need or the point of it. Can you try an explain a bit more about what benefit you would see from converting even one regular class to a Singleton?
(*) OK, the Singleton pattern as written actually allows for a specific predefined number of instances, but that's not really germane to this argument.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Hung Tang
Ranch Hand

Joined: Feb 14, 2002
Posts: 148

1. There is no synchronized code in my Singleton class.And suppose 1 request is accessing this class.At the same time,another request for this class come.Is the 2nd request queued till 1st request is over?

My first inclination is that the answer is no.
I don't think you can guarantee the order for which request gets priority over the other.

2.I believe that simultaneous request can access singleton class simultaneously.Hence in any application, all classes like service classes, DAO classes, except EJBs, should be singleton. This will improve memory perfomance as number of objects are significantly less.
Pls correct me if I am wrong.

Unless your DAOs have only read methods, I don't think it's a good idea to make them singleton classes. Even if you choose to make them singleton classes, you have better synchronize the methods that will make modifications to your data source.
Well, you might as well just return new DAO instances then because I don't think you'll like to be bothered with multi-threaded practices, and the issues that it comes with. The performance is usually negligible. For service classes, I would rather have non-singletons for those as well.
Hung Tang
Ranch Hand

Joined: Feb 14, 2002
Posts: 148

... or ensure all access to shared data is "synchronized". Singleton objects are not immune to this rule.

Singleton objects that are immutable, I think, is immune to this rule.
Sandeep Lodhia
Ranch Hand

Joined: Oct 09, 2000
Posts: 105
Guys,
1.First help me in understanding how multiple,SIMULTANEOUS requests for singleton class are handled.
I have a singleton class with NO METHODS SYNCHRONIZED.
Scenario : 1 request is already accessing a method A().Before 1st request has finished processing, 2nd request for same mathod A() comes.
Will 2nd request be put in a queue till 1st request is over?
Single instance of class would mean single instance of all its variables and methods.Then how simultaneous requests are handled in multi-threaded environment.
2.Purpose behind making DAO and Service classes is improvement in memory performance.Since there will be only 1 instance of each class,number of objects across application will be very less.
3.Pls suggest me explanation or link for disadvantages of using Singleton class WITHOUT SYNCHRONIZED CODE in it.
Thanks.
Sandeep.
[ June 04, 2003: Message edited by: Sandeep Lodhia ]
Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
If a method is not marked as synchronised then multiple threads can SIMULTANEOUSLY perform that operation. Threads will only wait or be queued when they are attempting to perform a section of code that is marked as synchronised and already locked by another thread.
As a consequence of this, if you are accessing a singleton class that modifies the values of instance variables then this section of code MUST be marked as syncronised to ensure that you do not encounter race-conditions or lost updates.
The Singleton pattern should be used sparingly and only for occasions when they should only ever be ONE instance e.g. typically used to control access to shared resources like a connection pool.
HTH


Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Sandeep Lodhia
Ranch Hand

Joined: Oct 09, 2000
Posts: 105
Perfect reply Andy.I understood it completely !!!
U cleared all of my doubts for singleton pattern !!!
 
GeeCON Prague 2014
 
subject: A Performance doubt on Singleton Pattern