Thread Priority In Java

Every thread is assigned a priority level, indicated by a numerical value ranging from 1 to 10. Typically, the thread scheduler manages the threads based on their priorities, a concept known as preemptive scheduling. However, the actual scheduling method employed can vary as it is determined by the specifications of the JVM. It is important to note that in addition to the JVM's default scheduling behavior, a Java developer also has the ability to manually assign priority levels to threads within a Java program.

Setter & Getter Method of Thread Priority

Now, we will explore the setter and getter functions for adjusting the priority of a thread.

The method getPriority in java.lang.Thread is a final method that retrieves the priority of a specified thread.

The method setPriority(int newPriority) in Java's java.lang.Thread class is used to modify the priority of a thread to the specified newPriority value. An IllegalArgumentException will be thrown if the newPriority value is not within the range of 1 (minimum) to 10 (maximum).

3 constants defined in Thread class:

  • public static int MIN_PRIORITY
  • public static int NORM_PRIORITY
  • public static int MAX_PRIORITY

The default priority level for a thread is set at 5, which is known as NORM_PRIORITY. The minimum priority value is 1, while the maximum priority value is 10.

Example of priority of a Thread:

FileName: ThreadPriorityExample.java

Example

// Importing the required classes
import java.lang.*;

public class ThreadPriorityExample extends Thread 
{

// Method 1
// Whenever the start() method is called by a thread
// the run() method is invoked
public void run()
{
// the print statement
System.out.println("Inside the run() method");
}

// the main method
public static void main(String argvs[])
{
// Creating threads with the help of ThreadPriorityExample class
ThreadPriorityExample th1 = new ThreadPriorityExample();
ThreadPriorityExample th2 = new ThreadPriorityExample();
ThreadPriorityExample th3 = new ThreadPriorityExample();

// We did not mention the priority of the thread.
// Therefore, the priorities of the thread is 5, the default value

// 1st Thread
// Displaying the priority of the thread
// using the getPriority() method
System.out.println("Priority of the thread th1 is : " + th1.getPriority());

// 2nd Thread 
// Display the priority of the thread
System.out.println("Priority of the thread th2 is : " + th2.getPriority());

// 3rd Thread 
// // Display the priority of the thread
System.out.println("Priority of the thread th2 is : " + th2.getPriority());

// Setting priorities of above threads by
// passing integer arguments
th1.setPriority(6);
th2.setPriority(3);
th3.setPriority(9);

// 6
System.out.println("Priority of the thread th1 is : " + th1.getPriority());

// 3
System.out.println("Priority of the thread th2 is : " + th2.getPriority());

// 9
System.out.println("Priority of the thread th3 is : " + th3.getPriority());

// Main thread

// Displaying name of the currently executing thread 
System.out.println("Currently Executing The Thread : " + Thread.currentThread().getName());

System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority());

// Priority of the main thread is 10 now
Thread.currentThread().setPriority(10);

System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority());
}
}

Output:

Output

Priority of the thread th1 is : 5
Priority of the thread th2 is : 5
Priority of the thread th2 is : 5
Priority of the thread th1 is : 6
Priority of the thread th2 is : 3
Priority of the thread th3 is : 9
Currently Executing The Thread : main
Priority of the main thread is : 5
Priority of the main thread is : 10

In cases where two threads possess identical priority levels, the Java thread scheduler plays a crucial role in managing their execution order. It is essential to understand how the thread scheduler functions in scenarios where thread priorities are equal. Let's explore an example to gain insights into the behavior of threads with matching priorities.

FileName: ThreadPriorityExample1.java

Example

// importing the java.lang package
import java.lang.*;

public class ThreadPriorityExample1 extends Thread 
{

// Method 1
// Whenever the start() method is called by a thread
// the run() method is invoked
public void run()
{
// the print statement
System.out.println("Inside the run() method");
}


// the main method
public static void main(String argvs[])
{

// Now, priority of the main thread is set to 7
Thread.currentThread().setPriority(7);

// the current thread is retrieved
// using the currentThread() method

// displaying the main thread priority
// using the getPriority() method of the Thread class
System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority());

// creating a thread by creating an object of the class ThreadPriorityExample1
ThreadPriorityExample1 th1 = new ThreadPriorityExample1();

// th1 thread is the child of the main thread
// therefore, the th1 thread also gets the priority 7

// Displaying the priority of the current thread
System.out.println("Priority of the thread th1 is : " + th1.getPriority());
}
}

Output:

Output

Priority of the main thread is : 7
Priority of the thread th1 is : 7

In a scenario where two threads possess identical priorities, it becomes unpredictable which thread will be chosen for execution first. The selection is determined by the algorithm implemented by the thread scheduler, such as First Come First Serve or Round-Robin.

Example of IllegalArgumentException

In the event that the newPriority parameter value in the getPriority method exceeds the range of 1 to 10, an IllegalArgumentException will be thrown. This scenario will be demonstrated through an example.

FileName: IllegalArgumentException.java

Example

// importing the java.lang package
import java.lang.*;

public class IllegalArgumentException extends Thread 
{

// the main method
public static void main(String argvs[])
{

// Now, priority of the main thread is set to 17, which is greater than 10
Thread.currentThread().setPriority(17);

// The current thread is retrieved
// using the currentThread() method

// displaying the main thread priority
// using the getPriority() method of the Thread class
System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority());

}
}

Upon running the aforementioned program, the exception displayed is as follows:

Example

Exception in thread "main" java.lang.IllegalArgumentException
	at java.base/java.lang.Thread.setPriority(Thread.java:1141)
	at IllegalArgumentException.main(IllegalArgumentException.java:12)

Input Required

This code uses input(). Please provide values below: