Open In App

Java Thread Priority in Multithreading

Last Updated : 19 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Java being Object-Oriented works within a Multithreading environment in which the thread scheduler assigns the processor to a thread based on the priority of the thread. Whenever we create a thread in Java, it always has some priority assigned to it. Priority can either be given by JVM while creating the thread or it can be given by the programmer explicitly. 

Priorities in Threads

Priorities in Threads in Java is a concept where each thread has a priority in layman’s language one can say every object has priority here which is represented by numbers ranging from 1 to 10 and the constant defined can help to implement which are mentioned below.

Constant

Description

public static int NORM_PRIORITY

Sets the default priority for the Thread. (Priority: 5)

public static int MIN_PRIORITY

Sets the Minimum Priority for the Thread. (Priority: 1)

public static int MAX_PRIORITY

Sets the Maximum Priority for the Thread. (Priority: 10)

In case, we need to set Priority with a specific value(between 1-10) we will need some methods for it. Let us discuss how to get and set the priority of a thread in Java.

  • public final int getPriority(): java.lang.Thread.getPriority() method returns the priority of the given thread.
  • public final void setPriority(int newPriority): java.lang.Thread.setPriority() method changes the priority of thread to the value newPriority. This method throws IllegalArgumentException if the value of parameter newPriority goes beyond the minimum(1) and maximum(10) limit.

Example 1: Setting and Getting Thread Priorities

Java
import java.lang.*;

class Thread1 extends Thread {

    // run() method for the thread that is called as soon as start() is invoked for thread in main()
    public void run()
    {
        System.out.println(Thread.currentThread().getName() + " is running with priority "  + Thread.currentThread().getPriority());
    }

    public static void main(String[] args)
    {
        // Creating random threads with the help of above class
        Thread1 t1 = new Thread1();
        Thread1 t2 = new Thread1();
        Thread1 t3 = new Thread1();

        // Display the priority of above threads using getPriority() method
        System.out.println("t1 thread priority: " + t1.getPriority());
        System.out.println("t2 thread priority: " + t2.getPriority());
        System.out.println("t3 thread priority: " + t3.getPriority());

        // Setting priorities of above threads by passing integer arguments
        t1.setPriority(2);
        t2.setPriority(5);
        t3.setPriority(8);

      	// Error will be thrown in this case t3.setPriority(21);

        // Last Execution as the Priority is low
        System.out.println("t1 thread priority: " + t1.getPriority());

        // Will be executed before t1 and after t3
        System.out.println("t2 thread priority: " + t2.getPriority());

        // First Execution as the Priority is High
        System.out.println("t3 thread priority: " + t3.getPriority());

        // Now Let us Demonstrate how it will work According to it's Priority
        t1.start();
        t2.start();
        t3.start();
    }
}

Output:

t1 thread priority: 5
t2 thread priority: 5
t3 thread priority: 5
t1 thread priority: 2
t2 thread priority: 5
t3 thread priority: 8
Thread-1 is running with priority 5
Thread-2 is running with priority 8
Thread-0 is running with priority 2

Explanation:

  • Thread1 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=2, t2=5, t3=8 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.

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.

Example 2: Threads with the Same Priority

Java
import java.lang.*;

// Extending Thread class
class ThreadDemo extends Thread 
{
    // run() method for the thread that is invoked as threads are started
    public void run()
    {
        System.out.println("Inside run method");
    }

    public static void main(String[] args)
    {
        // Main Thread Priority set to 6
        Thread.currentThread().setPriority(6);

        // Print and display main thread priority using getPriority() method of Thread class
        System.out.println("Main thread priority: "
                           + Thread.currentThread().getPriority());

        // Creting Thread inside Main Thread
        ThreadDemo t1 = new ThreadDemo();

        // t1 thread is child of main thread so t1 thread will also have priority 6

        System.out.println("t1 thread priority: " + t1.getPriority());
    }
}

Output
Main thread priority: 6
t1 thread priority: 6

Explanation:

  • The ThreadDemo class extends Thread and overrides the run() method to print a message.
  • In main(), the main thread’s priority is explicitly set to 6.
  • The main thread’s priority is printed using getPriority().
  • A new thread t1 is created inside the main thread.
  • Since t1 is created by the main thread, it inherits the main thread’s priority (6).

Note: Sometimes, thread priorities have minimal effect on the scheduler in different systems. To enforce strict priority-based scheduling in HotSpot JVM, we can set the system-level flag -XX:ThreadPriorityPolicy=1.


Article Tags :