Thread Priority in Java

Naveen AnamaneniUpdated · 3 min read

Every Java thread has a priority, a number from 1 (lowest) to 10 (highest). The priority is a hint to the thread scheduler that some threads are more important than others. By default, a thread has the same priority as the thread that created it, usually 5. However, the operating system is free to ignore priorities, so they must not be used to make a program correct. In this lesson, you will learn how to read and set priorities, and why they matter so little in practice.

Priority Constants

ConstantValue
Thread.MIN_PRIORITY1
Thread.NORM_PRIORITY5 (the default)
Thread.MAX_PRIORITY10

Methods

MethodDescription
setPriority(int p)It is used to set the priority (1 to 10); other values throw IllegalArgumentException.
getPriority()It returns the priority.

Example: Setting Priorities

Let's see an example that sets and reads priorities.

Priorities.java▶ Try it
public class Priorities {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("main priority: "
            + Thread.currentThread().getPriority());
 
        Thread low = new Thread(() -> { }, "low");
        Thread high = new Thread(() -> { }, "high");
        low.setPriority(Thread.MIN_PRIORITY);
        high.setPriority(Thread.MAX_PRIORITY);
        System.out.println(low.getName() + ": " + low.getPriority());
        System.out.println(high.getName() + ": " + high.getPriority());
 
        Thread child = new Thread(() -> System.out.println(
            "child inherits: " + Thread.currentThread().getPriority()));
        child.start();
        child.join();
 
        try {
            low.setPriority(11);
        } catch (IllegalArgumentException e) {
            System.out.println("Priority 11 rejected");
        }
    }
}
Output
main priority: 5
low: 1
high: 10
child inherits: 5
Priority 11 rejected

Explanation: The main thread has the normal priority, 5. Priorities can be set from 1 to 10, and a new thread inherits its creator's priority. A value outside the range is rejected.

Why Priorities Are Unreliable

  • On many systems, Java priorities are mapped onto a few operating-system levels, or ignored completely.
  • On a computer with free cores, every runnable thread runs anyway, whatever its priority.
  • Virtual threads (Java 21) always have priority 5; setPriority has no effect on them.

Common Mistakes

Using priorities to control order. Use join, locks or queues.

Setting all threads to MAX_PRIORITY. It changes nothing and can starve system threads on some platforms.

Practice

  1. Easy: Print the priority of the main thread and of a new thread created from it.
  2. Harder: Start one MIN_PRIORITY and one MAX_PRIORITY thread that each count to 100 million, and compare which finishes first over several runs. Explain your results.

Quick check: Thread priority

4 questions
1What is the default thread priority?
2What is the range of valid priorities?
3Does a higher priority guarantee a thread runs first?
4What priority does a new thread get?

Key Takeaways

Was this tutorial helpful?

Lesson 388 of 482