Awesome.....Haven't seen this indepth singleton implementation, mostly people just do a first level of implementation without thinking of the multithreaded environment.
Not sure why you don't have much more views for this video. I don't think anyone explained the multi thread issue in any of the singleton pattern video. Great job and thank you.
Cool video! It would be dope if you could also explain an alternative approach of making a singleton using enums please. Oh, and numbering the videos would be really beginner-friendly and much appreciated by the viewers!
I just found out about this channel and subscribed to it. Great Job! Although I think your volatile explanation is a bit wrong. I think volatile tells the OS that this variable will always be written/read to/from the RAM instead of the CPU cache memory. So let's dive in the example where we don't have volatile: Thread A enters the synchronized block and checks if instance is null (still haven't started initialization), at the same time thread B checks if instance is null outside the synchronized block and sees the instance to be null. Therefore thread B also tries to enter the synchronized block but will have to wait for thread A. Thread A after a successful initialization will exit the synchronized block and eventually return. But thread B on the other hand, inside the synchronized block will again see the instance to be null (since the instance variable might only be stored in the cache of a different CPU core where thread A was operating, and where thread B can't see), and will initialize it again. This will not be happening if the "instance" variable is declared volatile: Thread A enters the synchronized block and checks if instance is null (still haven't started initialization), at the same time thread B checks if instance is null outside the synchronized block and sees the instance to be null. Therefore thread B also tries to enter the synchronized block but will have to wait for thread A. Thread A after a successful initialization (will write instance to RAM directly instead of the CPU cache) will exit the synchronized block and eventually return. Now thread B, when checking again if the instance is null inside the synchronized block, will see that it is already initialized (since it will read directly from RAM) and will return that value. Correct me if I am wrong
Thank you for the support! I am afraid you are, it is exactly like I explained in the video, I suggest you watch that part again and if you still have trouble please let me know how can I improve my explanation in the future :) Additionally, feel free to check these references, maybe their approach will be better than mine at explaining the subject: stackoverflow.com/questions/11639746/what-is-the-point-of-making-the-singleton-instance-volatile-while-using-double-l (first answer in this Stack Overflow thread) or en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java (The part where they explain the "Broken multithreaded version"). Hope this helps! Cheers!
So with this pattern you can replace a single simple method that instanciate 10 different classes (implementing the same interface/abstract class) in a switch by 10 new creator classes to respect "Open to extension Close to modification" .... That's AMAZING ... What a great idea that's so much better now. LOL
Good explanation , one question if some one calls clone method on the singleton instance , it will create another object right ? do we have to override clone method and return same instance , please explain ?
After instance was initialised, it doesn't matter anymore what String data we pass as parameter to getInstance, it will always return the instance with the data when the method was called the first time. It this really the correct way of doing that?
Very nice! This video was mainly about the Singleton Pattern that is why I didn't want to delve other details. But what you could do is create a map and store several singletons based on their names, then the single instance would be tied to that name. So, in the most common example if you were using the singleton for DB instances and you were using two of them, you'll have two records in your map! Hope this helps :) Cheers!
Nope :) This is equivalent to result = new Singleton(data); instance = result; we need to assign the new Singleton created to the instance we have and not just the result being returned or else we will be fetching that result and creating a new instance every time! Cheers!
If the singleton needs to call a web service to fetch the singleton data such as access token, where do you place the logic to invoke the web service? Do you put it inside the singleton class or outside? Please clarify.
6:24 I don't get how volatile fixes the problem. It only makes thread B reread the variable from memory when returning it but the variable could still not be fully initialized by thread A.
What is the use of "private String data" field here? If i call the getInstance() function with some data string, it will not update the data field of the instance. The first getInstance() call will set the data value and it will never change again. What am I missing?
Nevermind, the question is already asked before and answered well. If anyone thought the same thing: A: Very nice! This video was mainly about the Singleton Pattern that is why I didn't want to delve other details. But what you could do is create a map and store several singletons based on their names, then the single instance would be tied to that name. So, in the most common example if you were using the singleton for DB instances and you were using two of them, you'll have two records in your map! Hope this helps :) Cheers!
No, it's just to check again if an instance is already created while the current thread was waiting for lock to acquire after the first check, 2nd check will be rarely used as most of times first check will be not null and return from there, double check is just for initialisation if many threads are trying to initialise the instance at the same time.
Very nice job. But we can still break this by serialize the object and deserialize it to create a duplicate object. I guess you should have covered that particular scenario on how to overcome that.