Java Thread Priority: Managing Thread Execution with Priority Levels

Learn how to set and retrieve thread priority in Java, with priority levels ranging from 1 to 10. Understand how the JVM scheduler uses thread priority to influence thread execution, optimizing task management in multithreaded applications.



Priority of a Thread (Thread Priority)

Each thread in Java has a priority, represented by a number from 1 to 10. The Java Virtual Machine (JVM) scheduler typically schedules threads according to their priority, though it's not guaranteed and depends on the JVM implementation.

Setter & Getter Method of Thread Priority

Java provides methods to set and get the priority of a thread:

Syntax

public final int getPriority()
public final void setPriority(int newPriority) throws IllegalArgumentException
    

Constants in Thread Class

The Thread class defines three constants:

  • MIN_PRIORITY: Minimum priority value (1)
  • NORM_PRIORITY: Default priority value (5)
  • MAX_PRIORITY: Maximum priority value (10)

Example: Setting and Getting Thread Priority

ThreadPriorityExample.java

public class ThreadPriorityExample extends Thread {
public void run() {
System.out.println("Inside the run() method");
}

public static void main(String[] args) {
ThreadPriorityExample th1 = new ThreadPriorityExample();
ThreadPriorityExample th2 = new ThreadPriorityExample();
ThreadPriorityExample th3 = new ThreadPriorityExample();

System.out.println("Priority of th1: " + th1.getPriority());
System.out.println("Priority of th2: " + th2.getPriority());
System.out.println("Priority of th3: " + th3.getPriority());

th1.setPriority(6);
th2.setPriority(3);
th3.setPriority(9);

System.out.println("Updated priority of th1: " + th1.getPriority());
System.out.println("Updated priority of th2: " + th2.getPriority());
System.out.println("Updated priority of th3: " + th3.getPriority());

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

Thread.currentThread().setPriority(10);
System.out.println("Updated priority of the main thread: " + Thread.currentThread().getPriority());
}
}
    
Output

Priority of th1: 5
Priority of th2: 5
Priority of th3: 5
Updated priority of th1: 6
Updated priority of th2: 3
Updated priority of th3: 9
Currently executing thread: main
Priority of the main thread: 5
Updated priority of the main thread: 10
    

In this example, threads are created and their priorities are initially printed (default is 5). Priorities are then updated and printed again. The main thread's priority is also updated and displayed.

Example: Same Priority Scenario

ThreadPriorityExample1.java

public class ThreadPriorityExample1 extends Thread {
public void run() {
System.out.println("Inside the run() method");
}

public static void main(String[] args) {
Thread.currentThread().setPriority(7);

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

ThreadPriorityExample1 th1 = new ThreadPriorityExample1();
System.out.println("Priority of th1: " + th1.getPriority());
}
}
    
Output

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

In this scenario, both the main thread and a newly created thread have the same priority (7). The output demonstrates that the main thread's priority is inherited by the new thread.

Example: IllegalArgumentException

IllegalArgumentException.java

public class IllegalArgumentExceptionExample {
public static void main(String[] args) {
try {
    Thread.currentThread().setPriority(17);
    System.out.println("Priority of the main thread: " + Thread.currentThread().getPriority());
} catch (IllegalArgumentException e) {
    System.out.println("Exception: " + e);
}
}
}
    
Output

Exception: java.lang.IllegalArgumentException
    

This example demonstrates an IllegalArgumentException when attempting to set a thread priority outside the valid range (1 to 10).