File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Alternate to singleton class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Alternate to singleton class ?" Watch "Alternate to singleton class ?" New topic
Author

Alternate to singleton class ?

PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Please give your suggestions on this

Rather than using a singleton class , is it a good idea to use a synchronized blocks or methods

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30309
    
150

Usually it is preferable to do neither. That said, the best alternative usually depends on the problem you are trying to solve. Why do you need a singleton? Are you trying to protect some data?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Jeanne Boyarsky , I am trying to put the JNDI reference in a HashTable for caching purpose.
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Anybody on this pleas give your suggestions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

PavanPL KalyanK wrote:I am trying to put the JNDI reference in a HashTable for caching purpose.

To me, that doesn't have anything to do with singletons, or with synchronized blocks. And to me neither of those two things are alternatives for the other. Would you care to clarify your requirements?
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
paul thanks for the reply.

"And to me neither of those two things are alternatives for the other"

Why you mentioned the above point ."Both are used for single point of access to a resource know".
Parambir Singh
Ranch Hand

Joined: Sep 05, 2004
Posts: 40

Synchronized blocks are required when you want to prevent two or more threads to access a part of the code in simultaneously. Since you are only caching a JNDI reference, the only piece of code that will be run will be something like:



You don't need to synchronize this. And a singleton is a good choice for these kinds of scenarios.


SCJP, SCMAD
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Parambir Singh wrote:Synchronized blocks are required when you want to prevent two or more threads to access a part of the code in simultaneously. Since you are only caching a JNDI reference, the only piece of code that will be run will be something like:



You don't need to synchronize this. And a singleton is a good choice for these kinds of scenarios.


Good Point Pranabir.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Alternate to singleton class ?