hey guys this is Navia welcome back to navine automation Labs a quick video on Singleton pattern in Java it's a very famous interview question and people they actually get confused that how exactly we can achieve the single done pattern with Java what is the purpose of that and what exactly the process so Singleton pattern means that we are going to create the object of that class or we are going to instantiate the object or instantiate of that particular class only once it means we cannot create the multiple objects once the object is created only one copy will be shared with all the threads right so for example let's say this is my browser class which I really want to make it with the singl T the three steps that we have to follow the first we have to make sure that okay you are creating one private static instance variable of the class second one is that private Constructor that you are introducing so that no one can create the object of that class instantiation means that we are going to create the object it means with the private Constructor no one can create the object and we have to create one public static getter method or something like get instance method to provide the access to that particular instance of the or instance or the object of that particular class and then after that if you really want to have your own public individual methods you can have your public individual methods of the class as well so first what exactly I'm going to do it that I'm going to create one private static variable of this particular browser class let's say this is my instance variable that I have created whatever the variable name that you want to create you can create it here so for example let's see a browser variable that I have created and then we have to create a private Constructor also so let's create the private Constructor of this particular class Constructor name will remain same as and then we are not going to add anything in this particular private browser Constructor we are making it private so that no one can create the object of this particular class and then we are creating one public static method also that will be a kind of getter method I would say let's see get uh instance method that I'm going to create which will give me the instance of that particular browser so first it will initialize the object and then after that it will return so what exactly I'm going to do it I'm going to create one public static void and let's see one get instance method that I'm going to write it here and then this method is actually I'm putting one check here that if my browser is equal to equal to null then only you have to create the object of that particular class so I'm writing then only I'm going to initialize my browser here is equal to new browser here like that right and once this is done and then I'm simply going to return a browser instance variable from this particular method so instead of white that I'm going to write a browser from here and then if you have to have if you really want to have any public method of this particular class you can display that so let's see for example let's see one display a message method that I'm going to use it here and then I'm going to print let's see something that browser info that I'm printing it here so how exactly it works now I have created one test browser class and having the main method over here first of all can I create the object of this particular class see I'm creating one object BR is equal to a new browser I cannot do that here why let me just simple write it new browser here I cannot do it why because I'm having a private Constructor and private Constructor will block me to create the object outside of this particular class so no one can create the object of this browser class see that's why it's giving me the error here that Constructor browser is not visible this is not public in nature this is private in nature so in the Singleton class remember that you are introducing the private Constructor also here so which method I can access so only this particular method I can access and I have to make this method static in nature so that I can just simply Call It by using the class name here so what I'll do this is my class name and then I'm writing dot get instance method so get instance method will be called and get instance method says if your browser equal to equal to null yes the default value of this particular browser in the beginning is always null yes if it is equal to null then browser is equal to new browser create the object of this class and return the reference return the instance reference or reference variable or object reference variable of this particular class so that is what we are getting get instance method is giving me the browser reference and then after that reference whatever the method you want to call or any public method of the browser class you can call it here so we have this particular uh display method or display message method that we are calling and then this is what it is actually using it over here so that's why if I run this program from here and then you see run as Java application and then you see the console output it's printing my browser info on the console so what is the purpose of the singl ton pattern the purpose of the singl ton pattern to achieve only one object creation of the class it means instantiate the object only once that's why the name itself is single T pattern create the object only single time only one time over here now there are a couple of things that we have to understand here that what if we are using using in the multi-threading environment can we use it in the multi-threading environment yes we can use it then how exactly we can achieve let's see for example if I have n number of threads over here so let's see this is my thread number one this is my thread number two this is my thread number three this is my thread number four let's see four threads are there the first thread is actually going to use this particular method and then the browser is equal to equal to null yes create the object of this particular browser class and return the browser so same browser will be returned back to this particular guy now the second thread is coming so first thread will get the browser first thread is actually initializing the browser because my browser was null first time now the second thread is again going to call this particular method the second thread is also coming and second thread is saying that browser equal to equal to null no browser is not equal to equal to null now this time return the browser it means whatever the copy of the browser that first thread is having the same copy will be given to the second guy also then the third guy also then the fourth guy also and then n number of threads which are available will be given to each and every thread here but now what we have to do here is that we have to do a proper thread management we have to achieve the thread safety otherwise it will be all the threads can use this particular static method at the same time so in that case we are going to introduce one keyword that is called a synchronized keyword that we can use it over here to achieve proper thread safety here it means all these threats are coming one by one to access this particular method what do you mean by synchronized here we are introducing synchronize keyword if the first thread is using this the second thread and third thread and all the upcoming threads will access this particular get instance method one by one it means the first thread is using it it will lock that particular that particular method so lock will happen and once the lock is released then only second thread will come then only third thread will come in the picture now the second thread is using thread is using it will lock it once the lock is released then only thread third thread will be there now the third thread will access that and then once the third thread is getting the copy then only it will release lock will be released then only the fourth thread is actually trying to access that so that's why the synchronized keyword that we have used it over here now what we can do here is that this entire method is actually synchronized it will be a problem in the multi-threading environment Plus what if tomorrow n number of threads are there so it could be a performance overhead it could be a performance issue because all the threads are running in the sequential mode so instead of providing the synchronized keyword over here we can use some other strategies we can enhance or we can optimize it by using the double check synchronization or double check instantiation we can do it over here so what exactly I'm going to do it here that I'm going to add one more if condition once again that if your browser is equal to equal to null this is my first check this is my outer check and then I'm going to introduce one synchronized keyword here first of all I'm removing the synchronized keyword at the method level and then I'm saying that I really want to use the synchronized keyword for this particular single Den class so dot class that I'm going to use it here and this check again I'm putting it here so this is my inner check and this is my outer check here and then only this particular synchronized block is synchron rized it means while creating the object and while checking the condition then only I'm putting the synchronization here not at the method label here what is the advantage of that the advantage is that still there is no violation of a Singleton pattern we are still creating only one object copy for all the threads but we are doing a proper thread management we are improving and optimizing by removing the synchronized keyword over here then I'm saying that okay n number of threads they can come and then they can come and then they can use this particular get instance method let's see thread number one is coming again browser equal to equal to null yes so right now there is no check at the same time if I'm running in the parallel mode second thread also can come and the third thread is also coming it over here now for example the first thread is actually checking browser equal to equal to null let's see first time browser equal to null and then I'm saying okay synchronize this particular process creating the object only once so again the browser will be created the object will be created and the same browser will be returned back to this particular thread number one now by the time at the same time second thread or the third thread is also coming so second thread also will use the get instance method and third thread and let's see fourth thread also can come at the same time why because we have removed the synchronized keyword from here so they all can come or they can all attack the get instance method or try to access a get instance method at the same time but now this process is actually synchronized this is always a better optimization technique Plus without the violation of the single done pattern it still we are creating the object of browser class only once so let's see at the same time second thread is also coming second thread will check browser equal to equal to null no browser is not equal to null now in that case the get the previous copy of the browser which is already created then the same browser copy will be given to this guy same browser copy will be given to third guy and the same browser copy will be given to the fourth guy as well over here so instead of writing the synchronized keyword better you introduce one synchronized block here and then you can do one more thing here is that you can introduce the a volatile keyword over here as well which is making sure that the read operation for all the threads are happening properly and every thread is getting systematically copy or uh proper copy of this particular browser one by one without any problem right so introduce the volatile keyword over here as well that is actually all the read operations for all the threads are having properly here so it is always a good practice to introduce the volatile keyword for your browser instance variable so this is how we achieve the single turn pattern and again if you come back here and if you run it also here and then see this it is actually absolutely working fine it's printing it here tomorrow you run this particular code with the multi-threading environment or five threads or six threads all threads they can come any threads they can come and execute that particular code but only one copy of the browser will be created here so it means we are not violating the concept of Singleton pattern here now if you really want to run this particular program in the multi-threading environment so I'll do one thing let me just comment it out and then I'm just going to create multiple threads over here so here you can see that I have created Five different threads thread number 1 2 3 4 5 over here and then I'm using the thread class here and then I'm starting the thread and then I'm uh initializing the thread I mean starting the thread and after that I'm writing thread. jooin and then we are waiting for all the threads to complete the task and we just need to create one browser task also here with the runable interface we can use it so let's see what exactly I'm doing it here that I'm using this particular runable interface task and then I'm writing the same thing here that we have us used actually here again with the help of browser. get instance method and calling this particular display message and the same task that I'm giving to each and every thread here so this is my thread number one this is my thread number two and for all the five threads I'm assigning the same task here so let's use it here like this and then I'm starting my threads one by one and then I'm running it so when you run this in the multi-threading environment also we will it will make sure that okay only one object is created but here you can see that browser info is getting actually printed five times why because five threads is attacking this particular get instance method at the same time perfect so this is what a parall execution also we are doing it by using the multi-threading plus we are just creating only one object over here so what will happen this thread number one will start thread number will perform thread number one will perform the start method and it will perform this particular task over here and this task is actually calling the get instance method and then let's see creating the first time object and get the browser copy here and then after that we are calling the display message now the second thread is also coming second thread is also having the same task it will try to create the object of the class and then it says browser equal to null no this time browser is not equal to null because we have already initialized with the first thread and then it will get the same copy of the browser object and the same instance reference will be given over here to thread number two thread number three four and five and so on and all these threads can run in the parallel mode in any sequence it may vary the sequence also and then we're just uh introducing and waiting for all the threads to complete over here that's it so here we have tested with the multi-threading environment also and single thread environment also and we are not violating the concept of single turn so this is how we can use the single turn pattern here I can add one practical use case in the next video video that especially with the selenium which is which is a very popular singl pattern we can use Singleton pattern with the uh with the thread with the thread local class to achieve the parallel execution properly that I'll show you in the next video so I hope that you clear what do you mean by a single turn pattern and how to achieve that thank you so much