Hi Shrayansh, I have been following this Java series from sometime and just wanted to shout out that you have made this learning enjoyable for us. You really teach things from first principle which is just amazing. Keep up the amazing work.
@ajayreddy5349Ай бұрын
@Shrayansh 14:13 double-checked Locking (encountered 1 more scenario, please CORRECT ME IF I'M WRONG): Without volatile: 1) If Thread 1 completes the assignment instance = new DBConnection() but hasn’t fully constructed the instance (due to reordering), Thread 2 may see instance as non-null and refer to this partially constructed instance. 2) as you explained, If Thread 1 hasn’t yet updated the instance reference in a way that is visible to Thread 2 (because of cache inconsistency), Thread 2 might still see instance as null. Explanation for 1st case: 'new DBConnection()' happens in several steps: 1. Allocate memory for the Singleton object. 2. Construct the object (initialize its fields and execute the constructor). 3. Assign the reference to instance, marking it as non-null. Due to the absence of volatile, the Java compiler or processor may reorder these operations as: 1. Allocate memory. 2. Assign the reference to instance (now instance is non-null). 3. Construct the object. Now, if Thread 2 checks instance != null before construction is complete, it may see the instance reference as non-null but with an incompletely initialized Singleton object.
@umairalvi73828 ай бұрын
By declaring the inner class as static, it's only loaded into memory when it's referenced for the first time. This means that the instance of the Singleton class is created lazily, i.e., only when the getInstance() method is called for the first time. This helps improve performance by avoiding unnecessary early instantiation.
@ConceptandCoding8 ай бұрын
right
@rahulsangvikar79732 ай бұрын
14:13 because of the happens before guarantees, it will never happen that the second thread will find the object as null inside the synchronization block. Using volatile seems like an overhead as it will cause latency issues throughout the lifecycle of this application.
@bhuvanakuppusamy693323 күн бұрын
Very good content 😊
@sagarsingh-wb8ou11 ай бұрын
At 17:43 as INSTANCE_OBJECT is private then how can outside class access it as private variable can only be used within the same class.
@ConceptandCoding11 ай бұрын
we are using public get and set method to access the private variables
@manimanam-d5o5 ай бұрын
i to got the same doubt read this In Java, the `private` access modifier restricts access to the members of a class only within the class itself. However, the nested static class `EagerHelper` is a member of the outer class `Eager`. This means that the outer class `Eager` can access the `private` members of its nested class `EagerHelper`. Here's how it works step-by-step: 1. **Nested Class**: The `EagerHelper` class is a static nested class inside the `Eager` class. 2. **Private Static Final Field**: The `EagerHelper` class has a private static final field `obj` that is initialized with a new instance of the `Eager` class. 3. **Access from Outer Class**: The outer class `Eager` can access the `obj` field of its nested class `EagerHelper` because it's a member of the `Eager` class. So, even though the `obj` field in `EagerHelper` is `private`, it can still be accessed from the `Eager` class. This is a feature of Java's access control for nested classes. Here’s the explanation with the code: ```java public class Eager { public static Eager Eagerobj; private Eager() { // private constructor to prevent instantiation } // Static nested class private static class EagerHelper { // Private static final field private static final Eager obj = new Eager(); } // Public static method to get the singleton instance public static Eager getEager() { // Accessing the private field of the nested class return EagerHelper.obj; } } public class Main { public static void main(String[] args) { Eager a = Eager.getEager(); // You can add additional logic here to verify the singleton behavior if needed } } ``` In summary, the `Eager` class can access the `private` field `obj` of the nested static class `EagerHelper` because it’s within the scope of the `Eager` class, which is allowed by Java's access control rules for nested classes.
@indianhits1486 ай бұрын
well 2:38 how can you create object just inside class, it should atleast be inside any method know? like main or something
@afzalkhan228811 ай бұрын
you mentioned we have discussed about string pool, in which video? i couldn't find
@ConceptandCoding11 ай бұрын
check this : kzbin.info/www/bejne/h2fCm6Wsr5KFbdk
@umairalvi73828 ай бұрын
In the constructor we should do new List(petNamelist);
@code4logics Жыл бұрын
can you please cover Multithreading and Concurrency in-depth, Sir?would be big help
@ConceptandCoding Жыл бұрын
its in my bucket list
@rabindradocument8934 Жыл бұрын
Learn Brian Goetz book. It is awesome.
@digitalanalytics559010 ай бұрын
Great Video Shrayansh. Just a small question for the Immutable class section. Should we not do a deep copy in the parameterized constructor as well, just like the getter method for collection classes? Because the original object can be modified if someone makes an addition to the petNameList right?
@A_CupOf_Life2 ай бұрын
In Bill Pugh Solution, why we created instance_object as final, and also why aren't we using final keyword with other solutions (for instance ). Can you please explain this, it will be a great help.
@ch.chandrasekharreddy5543Ай бұрын
I hope we can use it without static as well but in that case the reference variable can't be in Constant (not in capital letters).
@ganeshmaharnawar77354 ай бұрын
great content
@PhoenixRisingFromAshes471 Жыл бұрын
awesome video
@TechSavyHere6 ай бұрын
We can create a new arraylist in the constructor and make a copy of the list and that would solve the problem too ., right?
@afzalkhan228811 ай бұрын
hi Sj, can you please cover a session that explain Strings in details?
@umairalvi73828 ай бұрын
I went through the community posts but couldnt find the notes of any class. Please tell where to find the notes ???????????
@ConceptandCoding8 ай бұрын
pls check Member community post. there you will find my post where i shared the notes (some are missing too)
@harshalgarg9656 Жыл бұрын
Please help in understanding volatile keyword. My though is all operation that jvm performs, heaps, stack are all inmemory(cache). All our operations are having inMemory. that how disk/persistent memory came in picture. Is there any disk memory associate with jvm?
@ConceptandCoding Жыл бұрын
Check this video: see if this help in understanding Volatile keyword and use. kzbin.info/www/bejne/q6HJo4SshMx2aJY
@satyajeetdas65775 ай бұрын
Writes anywhere when performed are eventually made flushed to Disk for persistent storage , so that in future the same change remain persistent , in general we do read and write from/to caches then Inmemory where writes are periodically flushed to disk . It is something like Write Ahead logs where schedulers are set to periodically make updates to disk . Volatile does the same thing it like instructing thread to read and write from/to memory skipping cache .
@dassneha1911 ай бұрын
If you are making a playlist, make them visible to each users otherwise there is no need ir you can sell your courses. I started following your channel and subscribed it too but here scene is different. One video available and the next is not( available for members with higher price) .. leaving it . Thanks
@ConceptandCoding11 ай бұрын
java is always available to members only from start. Few videos made open based on request as many engineers need to watch few videos before taking membership. And i dont think 159Rs is higher for complete in depth java playlist. But thats my view, but wish you good luck for your future endeavours.
@anishsingla-pg2pd5 ай бұрын
I would considering the depth of the videos,, it's heavily discounted
@nitinvarshneykiet4 ай бұрын
@@ConceptandCoding check negative comments on your videos and u will find a pattern. 😅
@code4logics Жыл бұрын
with CompleteableFuture and async calls and Listenable ..with such topics
@ConceptandCoding Жыл бұрын
sure, will cover those.
@siddharth97036 ай бұрын
Hi i started with java palylist but there is no proper sequence followed actually, threads are at last part ,but you used the concept here , did i miss something or you made threads earlier?
@karankaira4 ай бұрын
In singleton enum approach , where are we intializing INSTANCE variable ?
@dep24604 ай бұрын
They are initlised by default constructor
@karankaira4 ай бұрын
@@dep2460 but where are we doing INSTANCE = new DataBaseConnection()
@amiyamishra98589 ай бұрын
Hello Shrayansh, Thank you so much for such great explanation. One doubt - in lazy initialization as we have already made the variable to static then how come multiple threads working parallelly create 2 copy of it.. As it is class variable now. I mean to ask why making the constructor private & making the variable to static only does not help??
@ConceptandCoding9 ай бұрын
Making the variable static only ensures its shared across instances, but without lazy initialization control, multiple threads can create multiple instances before the variable is initialized, leading to duplicate instances. Making the constructor private prevents direct instantiation, ensuring lazy initialization is properly enforced.
@chukwukennedy70357 ай бұрын
Hello..thank @Shryansh for this educative and awesome content.. I have a question, please anyone who understands the question and has a clue to the answer can still please help me. The Question is: According to the Singleton session were different types of Singleton class was touched. If the Synchronized Singleton class is solving the two thread issue, which Lazy initialization is facing...Then how Eager initialization managing the two thread issue Synchronize Singleton class is solving.
@satyajeetdas65775 ай бұрын
In Eager Initialization, the dbconnection obj is already created so multiple threads can call the getInstance() method simultaneously , there is no synchronization maintained .
@rabindradocument8934 Жыл бұрын
Can you write subclass for singleton class
@akarshkumarsrit2518 Жыл бұрын
when we do eager initialisation, there wont be an issue of creating multiple instances of the same class right even if multiple threads are trying to get the object?
@ConceptandCoding Жыл бұрын
Yes bcoz it's static, it's a class object
@splinkerp2222 Жыл бұрын
We can do it by Reflections
@sammykumar1777 Жыл бұрын
Sir till when java playlist be completed?
@ConceptandCoding Жыл бұрын
By next month most probably
@umairalvi73828 ай бұрын
How Bill pugs implementation is thread safe ???
@ConceptandCoding8 ай бұрын
because it relies on the classloader to initialize the Singleton instance, ensuring that it is created only once in a thread-safe manner.
@umairalvi73828 ай бұрын
@@ConceptandCoding class loading in Java is inherently thread-safe. Can you please explain this statement???
@divyanshumaheshwari286810 ай бұрын
Great Video , Shreyansh ! If I want to join this channel wit Membership perks, will this be recurring payment every month??
@ConceptandCoding10 ай бұрын
yea but you can compete it within a month or download it too
@magesh2353 Жыл бұрын
in an immutable class the member variables need not be final in this implementation i guess, am i correct? coz we cant change the values anyways as there are no setters and also they are not directly accessable
@thespptechs2851 Жыл бұрын
Yes, since variables are private and we don't have setters then we don't need to mark them as final.
@magesh2353 Жыл бұрын
@@thespptechs2851 thnx man
@ConceptandCoding Жыл бұрын
hi right, its not required, but say its a good practice (just add another layer of protection to the fields)
@magesh2353 Жыл бұрын
@@ConceptandCoding hmm okay thanks man
@harshalgarg9656 Жыл бұрын
Bhaiji Lazy initialization me multiple thread bhi aa gaye same time par tab bhi 2 object kaise banenge?? conObject toh static hai na
@ConceptandCoding Жыл бұрын
2 threads will try to create object with "new" keyword, memory mein to 2 objects ban jayenge. But bcoz of static in code it will keep only 1 reference of the 2 objects created.
@harshalgarg9656 Жыл бұрын
@@ConceptandCoding So basically it will create 2 objects but both will reference to one only? Other object will be cleared by Garbage collector? If yes then anyway it solves our problem right?
@ConceptandCoding Жыл бұрын
@@harshalgarg9656 no that's defeat the purpose of Singleton class. 2 objects should not be created at any point of time. Couple of points: - If it's a very expensive to create object, it will be done 2 times. - GC will not run instantly. So it will be in memory might be for some time.
@Laxmansingh-tl9bq Жыл бұрын
@ConceptandCoding Each thread have their own stack memory and if we make object non-static then each individual thread will have its own unique instance variable. On the other hand, static members are the part of metaSpace (within heap memory) , so all thread can access only one static instance variable. If there is any inaccuracies in my understanding, then please guide me. Thank you sir! your tutorials have been immensely beneficial.
@roshanjha6466 Жыл бұрын
bhaiya, please increase the frequency of the vedio .. upto when we start springboot?? ..kindly respond
@ConceptandCoding Жыл бұрын
10 videos max Java videos left. After that I will start springboot. Generally every weekend i upload the Java video, this weekend I got some personal work. But i will upload Java video soon.
@shivamyadav4846 Жыл бұрын
in immutable class it should be this,petNameList = Collections.unmodifiableList(new ArrayList(petNameList)); please correct if I am wrong
@ConceptandCoding Жыл бұрын
Right
@Placement-bv9cg Жыл бұрын
what is the problem with eager and lazy intilaization can. u explain me clearly??
@ConceptandCoding Жыл бұрын
Pls check this kzbin.info/www/bejne/q6HJo4SshMx2aJY
@shivaraj17cm Жыл бұрын
What all do I get from your paid training sources?
@ConceptandCoding Жыл бұрын
With "Unlock LLD, HLD and Java" level, many interview questions with solutions and many Java videos will be opened up.
@aditigupta68707 ай бұрын
In the case of double locking can you please explain what will be the case when thread1 preempts after checking null inside synchronised block, its an imp point which i was hoping should have been mentioned in video but its not, it would have been better if things are really explained in a better way and in depth rather than just covering a bunch of topics otherwise it will be of no help in interviews, interviewer asked me this imp point which you missed entirely
@ConceptandCoding7 ай бұрын
kindly check this 28. BUG in Double-Checked Locking of Singleton Pattern & its Fix kzbin.info/www/bejne/q6HJo4SshMx2aJY