Fundamentals of Thread Priority in Java:
Though concurrent execution of threads is conceptually true, it is impractical. Most of the computer configurations encompass a single Central Processing Unit and hence only one thread can be executed at a time. So, the practical implementation of concurrency of threads is an illusion. The ordered execution of several threads on a single CPU is referred to as scheduling. Java supports a deterministic and simple scheduling algorithm called the fixed priority scheduling. The algorithm enables the scheduling of threads on the basis of their priority with respect to the other threads that can be executed.
At the time of the creation of threads in Java, the thread that creates a new thread induces the priority for the newly created thread. The setPriority method can also be used to set or modify the precedence of a thread after it is created. The thread priority in Java is an integer value that ranges between MIN_PRIORITY and MAX_PRIORITY. These constants are defined in the Thread Class. The higher the value of the integer, the higher the thread priority.
When several threads are ready for execution at any given time, a thread with the highest priority is chosen by the Java runtime for execution. A thread with the immediate lower priority will start its execution only after the higher priority thread stops or yields or becomes inexecutable for some reason. The scheduler uses a round-robin fashion when two threads with the same priority are to be run. The thread chosen in this fashion will be executed until one among the below-mentioned conditions is satisfied:
- A thread with higher priority is executable.
- The thread yields or exits the run method.
- The allocated time of the thread has expired on systems that facilitates time slicing.
Once any one of the above-mentioned conditions is satisfied, a thread with the next higher priority is given the chance of execution. This process continues until the Java interpreter exists.
Check out our free courses to get an edge over the competition.
The thread scheduling algorithm of the Java runtime system is also pre-emptive. When a thread with the highest priority among all the threads that can run becomes executable, the runtime system allows the execution of this new thread with the highest priority. In such cases, all other threads are said to be preempted by the thread with the highest priority.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
Setting and Getting thread priority in Java:
The default value of thread priority in Java is 5. However, the thread priority can be modified to any value ranging between 1 to 10. Java’s Thread class has three static variables of priority to the threads in a multithreading system. They are:
- public static int MIN_PRIORITY
- public static int NORM_PRIORITY
- public static int MAX_PRIORITY
public static int MIN_PRIORITY
It is the minimum value of the precedence of a thread in Java. The minimum thread priority in Java corresponds to the value ‘0’.
Check out upGrad’s Advanced Certification in DevOps
public static int NORM_PRIORITY
This refers to the default value assigned by the Java Virtual Machine to the thread priority. The normal thread priority in Java corresponds to ‘5’.
public static int MAX_PRIORITY
This variable corresponds to the maximum value to which the thread can be prioritized. The maximum value of thread priority in Java is 10.
Java facilitates the developer to check and assign or modify thread priorities. There are two methods in Java’s Thread class to support this task. They are:
- public final int getPriority():
This method is invoked to return the priority of a Java thread. It is a final and public method. It returns an int type value.
- public final void setPriority(int newPriority):
With the invocation of this method, the priority of a Java thread can be altered. It is also a final and public method. The method returns nothing and hence it is said to return a void type.
newPriority: This function accepts an int type argument. By calling this method, a developer can assign a new thread priority value by specifying an appropriate integer value in the argument. However, the integer value should be a number between 1 and 10. Suppose the value exceeds 10, an IllegalArgumentException is returned.
Check out upGrad’s Advanced Certification in Cyber Security
Limitations of Thread Priority in Java:
The execution of the highest priority thread is not assured by the Java thread scheduler. Sometimes, a thread with lower thread priority may also be executed by the scheduler to dodge starvation. Because of this, the thread priority in Java can be used only to impact the scheduling policy for the purpose of efficiency. Thread priority cannot be relied upon for determining the correctness of the algorithm.
Do you want to learn the core concepts in Java? The upGrad Executive Post Graduate Programme in Software Development – Specialisation in Full Stack Development course is an online 7 weeks program for beginners who want to explore software development careers. Attend live and interactive classes, practice sessions, and over 35 hours of content delivered by industry leaders to earn a completion certificate from upGrad.