• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

If two instances of Singleton class are created by two threads, which one will be valid?

 
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you didn't write a strong singleton pattern as the below one, it can be broken by multiple threads. Assume if multiple threads created two instances of this class in the memory, which one's address this "instance" will point later?


Thanks
 
Marshal
Posts: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arun Singh Raaj wrote:If you didn't write a strong singleton pattern as the below one . . .

It isn't a singleton. What you showed is a singleton, as you said, but if you can get two instances of it by any means fair or foul, it ceases to be a singleton. It is a faulty implementation and its behaviour is undefined and unpredictable; you cannot tell which instance will be used.
You know that some people think singletons are a bad idea, and even call them an antipattern?
 
Bartender
Posts: 20924
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It isn't a singleton. What you showed is a singleton, as you said, but if you can get two instances of it by any means fair or foul, it ceases to be a singleton. It is a faulty implementation and its behaviour is undefined and unpredictable; you cannot tell which instance will be used.



Repeated for emphasis.

Also, to bolster Campbell's rhetorical question: https://stackoverflow.com/questions/12755539/why-is-singleton-considered-an-anti-pattern
 
Arun Singh Raaj
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright!
In case of the above code pattern of singleton, may I know which object in the memory will be referenced by the variable? Do all of them stay alive and not get removed by GC?
 
Tim Holloway
Bartender
Posts: 20924
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object will remain alive as long as there is at least one reference to that object. Only when there is no code or other objects referencing that object does it become eligible for garbage collection.

Singleton characteristics - or the lack thereof - do not affect that.
 
Ranch Foreman
Posts: 3298
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code shown the the first post in this thread, there will only be one EagerInitializedSingleton created, regardless of how many threads try to create it or access it.
 
Campbell Ritchie
Marshal
Posts: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, OP realised he had a correctly‑written singleton. He was asking what would happen if he hadn't written it correctly.
ASR: in the case of your correctly‑implemented singleton, because the instance is marked static, it remains reachable as long as the class is reachable. So it will only be eligible for GC when there are no more live references to its class.
 
Mike Simmons
Ranch Foreman
Posts: 3298
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, OP realised he had a correctly‑written singleton. He was asking what would happen if he hadn't written it correctly.



Yes, that's what I had thought - and yet he was asking which object would be referenced, referring back once again to the original code which was written correctly.  It didn't make sense to me, so I thought it worth restating, that that particular code would not have a problem.  It would make much more sense to discuss some other code that does have a problem.

 
Arun Singh Raaj
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is the class:

Since the method is not synchronized, if multiple threads access this method at the same time, they may create their own copy of static variable, isn't it?
Then all of the duplicate static variables will stay alive?
 
Mike Simmons
Ranch Foreman
Posts: 3298
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.  For a static variable, there is only one variable, regardless of the number of threads.  However, if it is a mutable variable, then different threads may see different values before or after a change.  In this case, the variable is final, so it's not mutable, and synchronization is unneeded.  All threads will see the same value, the one that points to the instance that was created when this class was first initialized.
 
Campbell Ritchie
Marshal
Posts: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No; in your original post the static field is created when the class is loaded and before the method can be called.
 
Mike Simmons
Ranch Foreman
Posts: 3298
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun: OK, now you've changed the post I was replying to. (And that Campbell was replying to.)  The original version of your post had no code, so obviously I was referring to your original code, which was the only code posted so far.  Now you've posted more code, which changes the answer.  

Anyway, I'm heading home now.  Someone else can answer the new version of the question.
 
Campbell Ritchie
Marshal
Posts: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arun Singh Raaj wrote:. . . . . .

That is a faulty implementation of a singleton. Since you don't initialise the field, you are returning a different instance every time that method is called.
 
Rancher
Posts: 828
19
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
late night
 
Arun Singh Raaj
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That is a faulty implementation of a singleton. Since you don't initialise the field, you are returning a different instance every time that method is called.


Yes, sorry for the silly mistake. I would like to correct the code.

Now, if multiple threads access this class concurrently then, will they create multiple instances of it?
 
Campbell Ritchie
Marshal
Posts: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arun Singh Raaj wrote:. . . . I would like to correct the code. . . . if multiple threads access this class concurrently then, will they create multiple instances of it?

Probably, but not definitely, yes. I have seen such code before, described a a faulty implementation notorious for not working correctly. Find a copy of Effective Java by Joshua Bloch: page 17.
 
Arun Singh Raaj
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
please respond to another question of mine.
 
a wee bit from the empire
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!