 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you how you can set thread priorities in multi-threading in Java. Before starting, you can subscribe my YouTube channel by clicking on the link which is available at the bottom right corner of this video. You can download my app Tachymus that is available on Google Play. You can connect with me by following these details which are available in front of you. Now coming on to the topic. First, understand the concept. Then I will be demonstrating an example. So topic is thread priorities and thread scheduling. First point says every Java thread has a priority that helps the operating system to determine the order in which threads are scheduled. The Java priorities are in the range between main priority which is constant of 1 and max priority with is a constant value equal to 10. Informally threads with higher priority are more important to a program and should be allocated processor time before lower priority threads. However, thread priorities cannot guarantee the order in which threads execute. By default, every thread is given priority that is norm underscore priority whose constant value is equal to 5. Each new thread inherits the priority of the thread that created it. So I hope that with this simple demonstration you have understood there are three predefined priorities that is main, max and normal and these priorities ranges from 1 to 10. You can increase or decrease your priority level as per your requirement. There are two methods for thread priorities. One is set priority and second one is get priority. So first point says you can set the threads priority use the set priority method which is a member of thread class shown below. Final void set priority and in bracket or parenthesis you have to pass a level that is an integer value. So here level specifies the new priority setting for the calling thread. The value of level must be within the range of min priority and max priority that is between 1 to 10. You can obtain the current priority setting by calling the get priority method of thread class shown here. Final int get priority its return value is int so it will return current thread sorry current priority of the thread. Now let's move to an example so that you can understand this concept well. Here I have implemented a class clicker test which is implementing runnable interface. Then inside this class definition click variable is declared whose value is 0. Then private volatile boolean variable is declared whose name is running and its initial value is true. Then run method is defined run is responsible for multithreading so whenever you want to apply multithreading then your class must contain run method. Inside run method a loop is implemented which is while inside parenthesis running variable is mentioned if its value is true means running is true then click variable will be incremented by 1. If running variable is false then this loop will be terminated. Then second method stop is defined which is assigning false into running variable so this is the definition of clicker test class. Now for execution I have implemented another class which is test so public class test containing main method. Inside main method this line number 3 is important here this thread is class it is calling current thread method which is calling set priority method and here thread dot max priority is passed so it is setting max priority for this main method so that it will occupy CPU time mostly. Then two objects high and low are created of clicker class so I have to write clicker test because name of class is clicker test so two objects high and low both are created then object T1 is created of thread class it is connected with high which is object of clicker test class then with the help of set priority method thread dot normal priority plus 2 so normal priority is 5 and 5 plus 2 which is 7 so T1 dot set priority it is receiving 7 as argument so its priority level is 7 then T1 dot start so T1 dot start will call run method so here start method will internally call run method and run has started its execution whenever we execute this code so if I execute main method then this T1 dot start will automatically call run method and run will start incrementing this click variables value so if running variable is true then click plus plus will be performed and if running is false then only it will stop incrementing the value now T2 object is created of thread class which is connected with low variable then T2 dot set priority here priority is 5 minus 2 that is 3 so priority level is 3 so T2 dot start again T2 dot start will call run method so now run is called twice one from T1 dot start and second from T2 dot start so two processes are working simultaneously and third one is this main method so this code will start three threads simultaneously for execution now try and catch are defined so inside try a sleep method is called thread dot sleep so thread class contains another method that that is sleep here 10,000 millisecond is passed so this statement transfers main method into sleep mode so that another two threads can perform their rotations and for any interrupted exception this catch block is defined then high dot stop and low dot stop is called so high and low both are called both will call their stop methods so stop method is defined in clicker test class so stop is assigning false into running variable so you can see that stop method is assigning run sorry false in that variable running variable so whenever running variables values false then this loop will stop execution so after calling stop method run method will be terminated now again try and catch are defined in main here main thread is treated as parent thread and T1 and T2 are known as child threads so if you want to wait if you want that parent thread waits for child threads to complete their execution then you can call join method so T1 dot join and T2 dot join so here parent thread that is main method waits for termination of child threads if they have terminated terminated their execution then only control will be transferred to further instructions otherwise main method or main thread or parent thread will wait until child threads have terminated after their termination control will be transferred to these system dot out dot println statements these statements are printing values of low dot click and high dot click so priority level of low is 3 and priority level of high is 7 so run method of high priority will be executed more number of times as compared to low so now I am executing this code so that you can see the output now check the output you can count number of digits it is containing eight digits and it is containing nine digits low priority and high priority is containing ten digits so obviously high priority threads value is higher than low priority thread and this is because we have set different priority levels for h1 and sorry high and low so friends I hope you have understood how we can set different priority levels for different threads how we can transfer control of a thread into runnable to sleep mode and how we can apply join method so that parent thread can wait for child threads to terminate their execution one more thing that I have used in this class that is volatile so volatile is a special keyword which avoids copy of a variable that is created by the CPU so if CPU is accessing a particular location frequently it creates a copy of that location so if you don't want to create a copy of a location then you can use volatile keyword with that variable so we want every time that actual memory location of running variable will be used by this while loop so I have used volatile keyword to avoid copying of this variable that is running so friends this is a simple demonstration so that we can apply different priorities on multi-threaded environment you can implement this code at your own so that you can also understand how these concepts are working if you want to watch more programming related videos you can subscribe my youtube channel thank you for watching this video