Open In App

Java Thread Priority in Multithreading

Last Updated : 22 Oct, 2025
Comments
Improve
Suggest changes
93 Likes
Like
Report

Java allows multiple threads to run concurrently, and the Thread Scheduler decides the order of execution. Each thread has a priority between 1 and 10, which indicates its importance. Threads with higher priority are generally preferred by the scheduler, though execution order is not guaranteed.

thread-
Thread-Priority

Java provides three constant values in the Thread class:

  • Thread.MIN_PRIORITY (1): Lowest possible priority for a thread.
  • Thread.NORM_PRIORITY (5): Default priority assigned to a thread.
  • Thread.MAX_PRIORITY (10): Highest possible priority for a thread.

Setting and Getting Thread Priority

We can use the following methods of the Thread class to manage thread priority:

  • setPriority(int newPriority): This method is used to sets the priority of a thread.
  • getPriority(): This method is used to returns the current priority of the thread.
Java
class MyThread extends Thread {

    public MyThread(String name){
        super(name); 
        
    }

    public void run()
    {

        System.out.println(
            Thread.currentThread().getName()
            + " with priority "
            + Thread.currentThread().getPriority());
    }
}

public class Geeks{
    public static void main(String[] args){
        
        MyThread t1 = new MyThread("Thread-1");
        MyThread t2 = new MyThread("Thread-2");
        MyThread t3 = new MyThread("Thread-3");

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

        // Start threads
        t1.start();
        t2.start();
        t3.start();
    }
}

Output
Thread-2 with priority 5
Thread-1 with priority 1
Thread-3 with priority 10

Explanation:

  • MyThread extends Thread and overrides run(), which prints the thread’s name and priority.
  • Threads have default priority 5, which can be changed between 1–10 using setPriority().
  • The code sets t1=1, t2=5, t3=10 and prints their priorities.
  • On calling start(), each thread runs concurrently; higher priority may get preference, but actual order depends on the OS thread scheduler.

Note: The output order may vary because thread scheduling depends on the JVM and the underlying operating system.

If multiple threads have the same priority, their execution order is decided by the thread scheduler. The example below demonstrates this, followed by an explanation of the output for better conceptual and practical understanding.

Java
class MyThread extends Thread{
    
    public void run(){
        
        System.out.println(getName()
                           + " is running with priority "
                           + getPriority());
    }

    public static void main(String[] args){
        
        // Set main thread priority
        Thread.currentThread().setPriority(5);

        // Create two threads
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();

        // Both threads inherit main thread priority (5)
        System.out.println("t1 priority: "
                           + t1.getPriority());
        System.out.println("t2 priority: "
                           + t2.getPriority());

        // Start threads
        t1.start();
        t2.start();
    }
}

Output
Main thread priority: 6
t1 thread priority: 6

Explanation:

  • Both t1 and t2 inherit the main thread’s priority (5).
  • Calling start() runs both threads concurrently.
  • Threads with the same priority have equal chance of execution; order depends on the OS scheduler.



Article Tags :

Explore