Thread.setPriority() Method in Java

Last Updated : 8 Apr 2026

The Thread.setPriority() method is used to assign a priority level to a thread. It acts as a hint to the thread scheduler about the importance of that thread compared to others. Threads with higher priority are generally given preference for execution over threads with lower priority, although the exact behavior depends on the JVM and underlying operating system. Every thread has a priority which is represented by the integer number between 1 to 10.

To read more Thread in Java

Thread class provides the following three constant properties:

  1. public static int MIN_PRIORITY: It is the maximum priority of a thread. Its value is 1.
  2. public static int NORM_PRIORITY: It is the normal priority (or default) of a thread. Its value is 5.
  3. public static int MAX_PRIORITY: It is the minimum priority of a thread. Its value is 10.

We can also set the priority of thread between 1 to 10. This priority is known as custom priority or user defined priority.

Syntax:

Parameter: The method accepts a parameter which is the priority to set this thread.

Returns: The method does not return any value.

To read more Thread Priority in Java

Exception

IllegalArgumentException: This exception throws if the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.

SecurityException: This exception throws if the current thread cannot modify this thread.

Example 1: Maximum Priority Thread

Compile and Run

Output:

Priority of thread is: 10

Example 2: Minimum Priority Thread

Compile and Run

Output:

Priority of thread is: 1

Example 3: Normal Priority Thread

Compile and Run

Output:

Priority of thread is: 5

Example 4: User define Priority Thread

Compile and Run

Output:

Priority of thread t1 is: 4
Priority of thread t2 is: 7
running...

Example 5: When priority is greater than 10

Compile and Run

Output:

Runtime Error:
Exception in thread "main" java.lang.IllegalArgumentException
	at java.base/java.lang.Thread.setPriority(Thread.java:1137)
	at Main.main(Main.java:13)

Example 6: Setting Priority of a Thread

Compile and Run

Output:

Thread name: Thread-1, Priority: 5
Thread name: Thread-0, Priority: 1
Thread name: Thread-2, Priority: 10

Explanation

The program creates three thread objects (t1, t2, and t3) by extending the Thread class and sets their priorities using Thread.MIN_PRIORITY (1), Thread.NORM_PRIORITY (5), and Thread.MAX_PRIORITY (10) respectively, indicating low, normal, and high importance. When the threads are started, each thread executes the run() method and prints its current name and assigned priority.

Note: The output of the above program may be different every time when we run the program.

Example 7: Priority with Runnable Interface

Compile and Run

Output:

High Priority Thread Priority: 8
Low Priority Thread Priority: 3

Explanation

In the above program, we have created two threads using the Runnable interface and assigning them different priority levels, where t1 is given a lower priority value of 3 and t2 is assigned a higher priority value of 8, indicating their relative importance to the thread scheduler; when the threads are started, each executes the run() method and prints its name along with its priority using Thread.currentThread() method.

Example 8: Default Thread Priority

Compile and Run

Output:

Default Priority: 5

Explanation

The program demonstrates how Java assigns a default priority to a thread when no explicitly priority is set. A new Thread is created using a lambda expression and started without calling the setPriority() method, causing the JVM to automatically assign it the normal priority value of 5 (Thread.NORM_PRIORITY), and when the thread executes, it prints the priority of the currently running thread using Thread.currentThread().getPriority() method.

Example 9: Invalid Priority Value

Compile and Run

Output:

Exception: Priority should be between 1 to 10

Explanation

The program demonstrates how Java enforces valid thread priority values by throwing an IllegalArgumentException. An invalid priority is assigned, as in this program where a thread attempts to set its priority to 15, which is outside the priority range (1 to 10). The exception is caught using a try–catch block, preventing abnormal program termination and allowing a meaningful message to be displayed.