The Volatile Keyword in Java Explained with Example | Interview Question | Multithreading |

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so now let's learn a very important concept that is the volatile keyboard and it's often asked in a lot of sd2 interviews it's also used in Singleton pattern and it's not a very easy concept to grasp and very few people know about it right so if you are a fresher you're watching this video uh I can assure you that there's something you can learn and brag about your classmates because very few people will actually know about this and this is not a very hard concept to grasp however it is not very easy to figure out that where to use this particular keyword okay but I will try my best to let you guys understand this keyword so volatile is a keyboard in Java and now let's see when does this usage can actually come into play so Suppose there are two threads right and you know what happens is a thread interacts with your CPU right and the CPU in turn interacts with the main memory or the ram now let us introduce our friend cache which basically helps us in reducing the access time so we know that uh it is far more efficient for a CPU to access data from the cache than for CPU to access data from the main memory right and that's why cache comes into picture because it gives you fast data access time so what happens ideally is whenever there is a shared variable that exists in the memory when I say a shared variable you can consider the top of the stack right which we saw in the previous example right and that is The Shield variable because multiple because that variable exists in the main memory and multiple threads are actually trying to access that variable and work on it and maybe update it or you know whatever it wants to do with that particular shade variable so let's consider here we have a shared variable whose black variable and initially it is set to True okay so now what happens is this thread both these threads they don't directly read from the memory they have their own cache and they read all these threads read from read the value of this flag variable locally from their cache now the problem that might happen is if let's say thread 2 changes the value of this flag to false it won't directly update it into the ram it would first update in its local cache right so you can see here it updates the flag variable to false but the other thread still can see the value of the thread as true because it is not updated in its local cache as well as in the main memory it is still true now next step it will take some time for this value of the cache to be propagated to the main memory as false because there was an updation that was being done by thread 2 but you can see the thread one still doesn't have num any visibility or that hey this flag is actually changed to false in order to get rid of this problem we introduce the volatile keyword if we declare the same variable as volatile that is volatile Boolean flag equals to true now what happens is this threads no longer read it from the cache or from the local copy they directly read it from the main memory as a result if the thread 2 changes if the thread 2 changes this flag to false the thread one will have access to it right just in case where let's say there is there is the status right there's a status flag uh that is constantly getting updated by multiple threads and based on the status Flags the other threads are doing some work right I mean the condition of that status flag will actually direct that how the threads will behave and is very important for all the threads to to have a consistent state of that particular flag variable I will give you an example if you remember my Singleton pattern video uh if you haven't checked out my singular design pattern video I would highly recommend you to go and check out that video but I will just give you a okay so now let's take a look at the Singleton class that is the TV set which we created while I was recording my signal and design pattern video so here what happens is what does a Singleton design pattern say I will just briefly tell you so in a Singleton design pattern you can create only one object of that particular class right so we have to design the class in such a way okay so that only one object can be created for that particular class so what we do is we maintain a static variable uh of the of the instance right and initially we set this to null right because let's say that when this when there is no object created uh the instance is null right and we declare a private Constructor we create a private Constructor because we don't want uh any other client to instantiate this class from outside okay so if now what if uh put any client one wants to instantiate this particular uh particular class right so for that we create a static method right now why do we make this Constructor private why do we make a static method of get instance all this I've covered in depth in that Singleton design pattern video uh and I'm not going to cover that in such that but for now just want to say that basically we don't expose our Constructor we expose this static method which basically checks that okay whatever this instance if this is null that means no object has been created for this particular class and that is we go ahead and we create an instance of this particular class and we before returning that instance to our client we store it we store it we update this reference variable uh to the new instance that we just created and so the next time if some other client wants to Again instantiate by calling this get TV instance object what we do is we return this particular this particular instance and we don't end go up go and create another instance right as you can see only if it is null only if this instance is null then only we create a new TV set otherwise we return otherwise we don't create any new TV set and we just return the same old instance and that way we ensure that only one instance of this object is always created right like again why this is synchronized why this is uh like why we are having two checks right all these things which are called which you call double check blocking and all these things have covered during the Singleton design pattern video so it is highly recommended to go and check out the video after you've watched this multi-threading videos but here you can see that there is a flag right there is a flag that is this TV set instance this this TV set instance is acting as a flag right why this is acting as a flag is it says that okay if this if this reference is null then create an object otherwise don't create an object return that same object so it is kind of acting as a flag right that that particular variable the state of the particular variable defines like the op the following operations right so now let's say in a multi-thread environment uh multiple threads are trying to access this class and let's say one of the threads one of the threads get access to this class and it Updates this TV set instance so it's the first thread comes in uh and first it comes in and it Stacks that okay this TV set instance is null then let me create a new TV set instance uh and I will and let me return it now let's say this first thread has created this new TV set instance and it has updated its value and it has returned it right now if you go back here you see that what this thread did it updated the TV set instance value in its local cache but it is not yet propagated to the main memory and let's say next is propagated to the main memory but it's still not propagated to the other threads cache so the other thread is now if you again one this other thread now wants to create a new instance it will find in its local cache that hey this TV set instance value is still none and has a result what it will do is it will again go and create a new object so now there are two objects being created which kind of violates the uh Singleton design pattern principle which says that only one instance of you know of this particular class can be created and that is the reason I want this this flag instance where you built to be directly updated in the main memory and directly read by the thread from the main memory and that is the reason we declare it volatile I've also explained in that video as well but this is more an extensive explanation on on volatile keyboard so I hope you're able to grasp this concept because it's a very very important concept uh it's not very easy to understand and well or to use volatile in all of the cases so that might require a lot of design discussions and profiling but yeah that's that's mostly about
Info
Channel: Riddhi Dutta
Views: 8,766
Rating: undefined out of 5
Keywords: java multithreading tutorial, java multithreading interview questions, java multithreading project, java multiple inheritance, java multithreading by durga sir, java multithreading and concurrency tutorial, java multithreading synchronization, java multithreading complete tutorial, java multithreading interview questions for 10 years experience, java multithreading program, java concurrency tutorial, java concurrency and multithreading interview questions, volatile, volatile keyword
Id: MtMRwUXG9Rc
Channel Id: undefined
Length: 7min 24sec (444 seconds)
Published: Sun Apr 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.