• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton guaranteed  RSS feed

 
Andrew Cane
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with this code:



Does this code guarantee singleton for Singleton class? thanks
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code won't even compile. There are many articles on the internet explaining good singletons and bad ones and what the singleton gotchas are so google some articles and read them.
 
Andrew Cane
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Sorry, missed a whitespace in getInstance method and a closing curved bracket. Now, does it work? thanks
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It still doesn't compile but even if you make it compile, how are people going to get an instance of your singleton outside your singleton class? What is going to happen when multiple threads try to get an instance of your singleton assuming you have now made it possible for others to use it? The articles on google explain all that and more.
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still doesn't compile. Try and compile it yourself first then cut and paste the working code. That's the least error prone way to do things.

However, let's assume that you have your syntax sorted and that it does compile ok. There is a problem in your design. Think about how you're actually going to instantiate one of these Singleton objects? The best approach would be to write some Unit Tests to verify its behaviour. You'll want to make sure you can first instantiate a Singleton object. Then you'll want to verify that if you obtain multiple references that they all refer to the same object.

Like E Armitage says, there are a bazzilion articles on the internet that show you how to do this.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a far better way to get a Singleton object:-Read all about it in the Java Tutorials and Language Specification.
 
Andrew Cane
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Sorry, I don't know why the whitespaces keep on missing.

@Campbell Ritchie:
Yes, I know enum is the best way for singleton, but I don't think enum can implement Runnable.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never said anything about Runnable. Have you tried it? Does it say anything in the language specification about implementing interfaces in an enum?

Why on earth do you want a singleton Runnable? If you want to create a single Thread, there are thread pool classes.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why on earth do you want a singleton Runnable?


Indeed. You could consider just creating a Runnable object. If you do it right you may not need the error-prone (you have read those tutorials by now, right?) machinery for ensuring there is only one.
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Cane wrote:




if this code compiles it must throw StackOverFlowException or something like that. Fortunately this code won't complie
At one time you create a singleton object it makes a another Singleton object for the previously mentioned variable.
and that object too has a private Singleton _object = new Singleton(); variable.
again and again Singleton class makes new Singleton(); instances.
This is the reason why this code won't compile.

Have you ever heard about Thread in java?
Then THINK what happens if 2 Threads try to get a Singleton instances in same time.

Hope now you have the answer
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!