GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java Thread Priority in Multithreading - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java Thread Priority in Multithreading

Last Updated : 24 Apr, 2026

Java supports multithreading, where multiple threads run concurrently and the Thread Scheduler decides their execution order. Each thread is assigned a priority (1–10) that influences scheduling but does not guarantee execution order.

  • Changing priority does not guarantee faster execution or immediate scheduling
  • Higher-priority threads are generally preferred by the scheduler
  • Actual execution order depends on the JVM and underlying OS
Java
class SimpleThread extends Thread {
    public void run() {
        System.out.println(getName() + " is running with priority " + getPriority());
    }
}

public class GFG {
    public static void main(String[] args) {
        SimpleThread t1 = new SimpleThread();
        SimpleThread t2 = new SimpleThread();

        t1.setName("HighPriorityThread");
        t2.setName("LowPriorityThread");

        t1.setPriority(Thread.MAX_PRIORITY); // 10
        t2.setPriority(Thread.MIN_PRIORITY); // 1

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

Output
LowPriorityThread is running with priority 1
HighPriorityThread is running with priority 10

Explanation:

  • Two threads are created: HighPriorityThread and LowPriorityThread.
  • HighPriorityThread is assigned maximum priority (10), and LowPriorityThread gets minimum priority (1).
  • When start() is called, both threads run concurrently.
  • Higher-priority threads may get more CPU time, but the actual execution order is not guaranteed because it depends on the JVM and the OS scheduler.

Thread Priority Constants

java_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 set 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: The program creates three threads, assigns them different priorities (1, 5, and 10), and starts them using start(). Each thread runs concurrently and prints its name and priority, but the execution order is not fixed and depends on the 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
t1 priority: 5
t2 priority: 5
Thread-1 is running with priority 5
Thread-0 is running with priority 5

Explanation: The main thread sets its priority to 5, and both t1 and t2 inherit this priority when created. When start() is called, both threads run concurrently and print their details, and since they have the same priority, their execution order depends on the OS scheduler.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.