Thread Priority

About

Thread priority in Java determines the order in which threads are scheduled for execution. However, it does not guarantee exact execution sequence—it is just a hint to the thread scheduler.

  • High-priority threads might execute before low-priority threads, but not always guaranteed.

  • OS-dependent behavior: Some JVMs may completely ignore priority settings.

Thread Priority Levels

Java defines three standard priority constants in the Thread class:

Constant

Value

Description

Thread.MIN_PRIORITY

1

Lowest priority

Thread.NORM_PRIORITY

5

Default priority

Thread.MAX_PRIORITY

10

Highest priority

Thread Priority Effects

Priority Level

Effect

Low (1-4)

Less CPU time, runs after high-priority threads.

Normal (5-9)

Balanced execution.

High (10)

May get more CPU time, but not guaranteed.

Setting Thread Priority

We can set a thread's priority using:

thread.setPriority(somePriorityValue);

And retrieve it using:

int p = thread.getPriority();

Default Thread Priority

  • The main thread has a default priority of 5 (NORM_PRIORITY).

  • A newly created thread inherits the priority of the thread that created it.

public class DefaultPriorityDemo {
    public static void main(String[] args) {
        System.out.println("Main thread priority: " + Thread.currentThread().getPriority());

        Thread t = new Thread(() -> System.out.println("New thread priority: " + Thread.currentThread().getPriority()));
        t.start();
    }
}

Output (default JVM behavior):

Main thread priority: 5
New thread priority: 5

Setting and Getting Thread Priority

class PriorityDemo extends Thread {
    public void run() {
        System.out.println("Thread Name: " + Thread.currentThread().getName() +
                ", Priority: " + Thread.currentThread().getPriority());
    }

    public static void main(String[] args) {
        PriorityDemo t1 = new PriorityDemo();
        PriorityDemo t2 = new PriorityDemo();
        PriorityDemo t3 = new PriorityDemo();

        t1.setPriority(Thread.MIN_PRIORITY);  // 1
        t2.setPriority(Thread.NORM_PRIORITY); // 5 (default)
        t3.setPriority(Thread.MAX_PRIORITY);  // 10

        t1.start();
        t2.start();
        t3.start();
    }
}

Possible Output (varies based on OS & JVM scheduler):

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

Priority May Not Always Work

Even if a thread has MAX_PRIORITY, the JVM does not guarantee execution order.

for (int i = 0; i < 5; i++) {
    Thread high = new Thread(() -> System.out.println("High Priority"));
    Thread low = new Thread(() -> System.out.println("Low Priority"));

    high.setPriority(Thread.MAX_PRIORITY);
    low.setPriority(Thread.MIN_PRIORITY);

    low.start();
    high.start();
}

Expected vs. Actual Output

High Priority
Low Priority
Low Priority
High Priority
High Priority
Low Priority
Low Priority
High Priority

Threads do not always run in expected order.

Usage of Thread Priorities

A payment processing thread gets higher priority than a logging thread.

Last updated

Was this helpful?