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

A Performance doubt on Singleton Pattern

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


... 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
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sandeep Lodhia
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect reply Andy.I understood it completely !!!
U cleared all of my doubts for singleton pattern !!!
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic