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:
We can also set the priority of thread between 1 to 10. This priority is known as custom priority or user defined priority.
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
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.
Output:
Priority of thread is: 10
Output:
Priority of thread is: 1
Output:
Priority of thread is: 5
Output:
Priority of thread t1 is: 4 Priority of thread t2 is: 7 running...
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)
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.
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.
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.
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.
We request you to subscribe our newsletter for upcoming updates.