Thread Priority in Java

Last Updated : 16 Jan 2026

In this chapter, we will learn how thread priorities work, how to set and retrieve them, and how Java handles threads with equal priorities.

What is Thread Priority in Java?

In Java, each thread is assigned a priority that helps the thread scheduler decide the order of execution. Thread priorities are represented by numeric values ranging from 1 to 10. Although higher-priority threads generally get preference during execution, the actual scheduling behavior depends on the JVM and the underlying operating system.

Thread Priority Values

Every thread has a priority value between 1 and 10:

  • 1 is the minimum priority
  • 10 is the maximum priority

In most cases, the thread scheduler follows pre-emptive scheduling, where higher-priority threads are executed before lower-priority ones. However, this behavior is not guaranteed, as thread scheduling is JVM-dependent. A programmer can explicitly assign priorities to threads within a program.

Setter and Getter Methods of Thread Priority

Java provides two built-in methods to work with thread priority:

1. getPriority() Method

The java.lang.Thread.getPriority() method returns the priority of the given thread.

Syntax

The syntax of the method is:

2. setPriority(int newPriority) Method

The java.lang.Thread.setPriority() method updates or assign the priority of the thread to newPriority. The method throws IllegalArgumentException if the value newPriority goes out of the range, which is 1 (minimum) to 10 (maximum).

Syntax

The syntax of the method is:

Thread Priority Constants

The Thread class defines three priority constants:

Here:

  • The default priority of a thread is 5 (NORM_PRIORITY)
  • Minimum priority is 1
  • Maximum priority is 10

Example: Priority of a Thread

The following example demonstrates how to get and set the priority of threads using the getPriority() and setPriority() methods.

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

We know that a thread with high priority will get preference over lower priority threads when it comes to the execution of threads. However, there can be other scenarios where two threads can have the same priority. All of the processing, in order to look after the threads, is done by the Java thread scheduler. Refer to the following example to comprehend what will happen if two threads have the same priority.

Threads with Same Priority

A thread with a higher priority usually gets preference over lower-priority threads. However, when two or more threads have the same priority, Java does not guarantee which thread will execute first. The decision depends entirely on the thread scheduler algorithm used by the JVM (such as First-Come-First-Serve or Round-Robin).

Example: Threads Having Same Priority

The following example demonstrates the behavior of threads when multiple threads have the same priority.

Output:

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

Explanation:

If there are two threads that have the same priority, then one can not predict which thread will get the chance to execute first. The execution then is dependent on the thread scheduler's algorithm (First Come First Serve, Round-Robin, etc.)

IllegalArgumentException in Thread Priority

If the value passed to the setPriority() method is outside the valid range (1–10), Java throws an IllegalArgumentException.

Example: IllegalArgumentException

The following example demonstrates how an IllegalArgumentException is thrown when an invalid priority value is assigned to a thread.

When we execute the above program, we get the following exception:

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