• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alternate to singleton class ?

 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34974
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky , I am trying to put the JNDI reference in a HashTable for caching purpose.
 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody on this pleas give your suggestions
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic