 Hello friends. I am Sanjay Gupta. In this video, I am going to demonstrate you the use of volatile keyword in Java. Before starting, you can subscribe my YouTube channel by clicking on the link which is available at bottom right corner of this video. You can download my app Takimus that is available on Google Play. You can connect with me by following these details which are available in front of you. Now for understanding purpose, first I am going to explain this code. Here you can see clicker test class is implemented and it is implementing runable interface. Then click variable is declared which is initialized by 0. Second variable is running whose value is true and it is private, volatile and boolean. So its data type is boolean and it is volatile in nature. Then two methods run and stop are defined. Run method is containing while you condition is variable running. If running variable is true then click plus plus will be performed. Otherwise this loop will not be repeated. Second method is stop which assigns false inside running variable. So this is a scenario where volatile keyword is used with running variable. Now understand what is the use of volatile. So you have noticed that running is preceded by the keyword volatile. Volatile ensure that the value of running is examined each time the following loop it rates. So while loop is checking value of running if it is true then click plus plus is performed otherwise loop will not be repeated. So with the use of volatile loop has to check running every time whenever it is changed or not. Without the use of volatile Java is free to optimize the loop in such a way that a local copy of running is created. So if we don't use volatile then a local copy of a variable is created. So without using volatile there are two copies. One is actual and one is local. If you change actual variable then it might be possible that local copy is not updated and you will receive indirect outputs or results. So if you want to avoid that problem you can apply volatile keyword so that local copy cannot be created. So the use of volatile prevents this optimization telling Java that running may change. Here running is a variable that we have used here. So running may change in ways not directly apparent in the immediate code. So it is saying that if actual copy of running is changed and you have not used volatile so it might be possible that local copy of running is not changed and you will receive incorrect outputs. So because of this problem I have used volatile keyword in the code that I have demonstrated you. You can see that here running is containing true and its nature is volatile. So volatile means actual copy of running variable will be checked here every time using while loop. No copy of running variable will be created. So because of volatile keyword it will be checked every time so you will be receiving correct output in all the cases. So this is the use of volatile keyword in Java. You can use this keyword whenever you don't want to create any copy of a variable and you want to check its original value. So I hope you have understood the use of volatile keyword in Java. If you want to watch more programming related videos you can subscribe my YouTube channel. You can follow my YouTube channel. Thank you for watching this video.