 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you how you can apply synchronization in multi-threading in Java. Before starting, you can note my information. 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 TechMS that is available on Google Play. You can connect with me by following these details which are available in front of you. Now I am going to demonstrate you an example which will explain you the concept of synchronization in multi-threading which is available in Java. So here you can see a code is available. Here name of classes call me. Inside call me class call method is defined which is receiving an argument MSG which is of string type. Then system.outprint statement is printing opening bracket and then masses which is received by this MSG argument. Then try is containing thread.sleep method. So sleep method will transfer control of this method into sleep mode. If any exception will occur then interpreted exception will be catch by this catch block and the masses interpreted will be printed. After completion of this sleep method duration control will be transferred to the system.out.println and it will display closing bracket on the screen. So this is the definition of call me class. It is containing only single method that is call. Now second class is caller. It is extending thread class. So it is extending thread class because it will be applying multi-threading concept and thread class contains run method. So caller class has to override run method that you can see here. It is overriding run method and defining some statements inside this method. Before that string MSG variable and call me object sorry call me reference target is declared here. Then caller. This is a constructor which is available here. This constructor is receiving an argument target and second argument s. Target is of call me type and s is of string type. Both the arguments are assigned into target and MSG variable. Then run method which is calling call method through target attribute. So target is of class me reference which is calling call method and MSG message which is received here into s. S is assigned into MSG. So this message is passed in call method. So call method is available inside this call me class which I have explained earlier. So caller class run method will call call method of call me class. So these both classes are linked together. So first class is call me which is containing call method. Second class is caller which is applying multi-threading concept and it is overriding run method which will call call method of call me class. Now third class is sin text. So synchronization test class is containing main method inside this method and object of call me class is created. Then object of caller class OB one is created which is receiving target as argument and hello as argument. So this target and hello these two arguments will be received by constructor of caller class. So I am moving to caller class. You can see here this constructor is receiving two arguments inside target and S. So target object will be received inside this target and hello will be received inside this S argument. Then OB one dot start method is called. So start method is responsible for calling run method. So this OB one dot start statement will call this run method and this run method will call this call method which is defined inside call me class. So I hope you have understood all the sequences. So OB one dot start is calling run method of caller class and run method of caller class is calling call method of call me class. Similarly, OB two object is created which is also receiving target and synchronized two arguments. It is also starting start. Sorry, it is also calling start method which will call run and run will call call method and similarly OB three. It is also calling start. It's start method will call run and run will call method. So three objects have been created inside main method. All three methods are calling start and their start calling method are calling run method and all run calls are calling call method. Now inside try block OB one dot join OB two dot join and OB three dot join methods are called. It is because of main method is waiting for child threads to complete their execution. Here child threads are created by this OB one OB two and OB three. So OB one OB two and OB three have called start and start have called run. So when run method will complete its execution, then only main method will be starting its execution after this point of time. So join method is responsible for waiting child thread to complete their execution. When child thread completes their execution, then this parent thread, which is main method will complete its execution. So now I'm going to execute this code. You can see the output. This demonstration right now is not using the concept of synchronization. That's why you are seeing a mixed output. First of all, opening bracket and hello is printed. Then again, opening bracket and world, then opening bracket and synchronized, then closing bracket closing bracket and closing bracket. So all three start methods initiated together, then they call their run and then they call their call methods. So all calling statements are working together and they all are accessing this call method. So this call method is shared by all the multi thread calling of run method. So now we have to apply synchronization on this call method. So if any particular thread is accessing this call method, then remaining threads do not access this call method that will be applied with the help of synchronized keyword, which is available in Java. So I am applying that keyword. So I have applied synchronized keyword with this call method. So right now this method is synchronized. So synchronized keyword put a lock on this call whenever this method is called by a thread, then a lock is maintained on this method so that other threads cannot access this call method. When a particular thread completes its execution, then only remaining threads will be given calling this method. So now I am again saving this code and executing the program. So you can see that now output is printed in synchronized manner. First opening bracket, hello, and then closing bracket. This is output of first thread, then the output of second thread, which is world and output of third thread, which is synchronized. And you can also see that first OB1 is called, which is having hello as a message, then synchronized and then work, but output is in different order. So in case of multi threaded environment or if you are implementing multi-threaded concept in Java, then your output will print in different orders. And if you are applying synchronization, then a particular thread can access a particular method at a particular time. If another thread wants to access that method, then permission will not be granted. If first thread has completed its task, then only another thread will occupy log to a particular method. So if you want to make your method synchronized, then you can use synchronized keyword like I have explained how we can use synchronized keyword in the Java and in multi-threaded programming. So I hope you have understood both the scenarios, how we can implement a program in Java without using synchronized keyword and using synchronized keyword. And I have demonstrated you both the outputs, what are the differences between them. So if you want to watch more programming related videos, you can subscribe my YouTube channel. Thank you for watching this video.