• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is my singleton implementation a true singleton?  RSS feed

 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting in on the fun... Could this implementation provide a rock solid singleton class?

 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that produces a threadsafe Singleton implementation, and I've seen implementations like that in my company.

It is a little awkward if your Singleton constructor can throw an Exception. Java gives a confusing ClassNotFound error if there is an exception thrown from a static initializer in a class.
 
Ranch Hand
Posts: 262
4
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No idea what you mean by a rock solid singleton, but reflection attack is possible with your implementation.



The output of the above mentioned code is --

false
1741555911
128087567
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed that is true. Is there a way to avoid a reflection attack without using a security manager? I think I can think of a way to update Rico's implementation to stop your attack working, but I don't have a compiler to hand to test it.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:Indeed that is true. Is there a way to avoid a reflection attack without using a security manager? I think I can think of a way to update Rico's implementation to stop your attack working, but I don't have a compiler to hand to test it.


Yes there is with ReflectPermisson and a proper policy file, but it's extremely rare to have to worry about such security considerations.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Informative
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:Getting in on the fun... Could this implementation provide a rock solid singleton class?

Sure. However, the need for such an idiom (I believe it's called "lazy initialization holder") is extremely rare.
It's also quite possible that it will be made redundant by future releases (several others already have been), in which case what you'll be left with is a piece of arcane code.

That's assuming you even need a singleton to begin with.

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!