A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-thread-priority-multithreading/ below:

Java Thread Priority in Multithreading

Java Thread Priority in Multithreading

Last Updated : 23 Jul, 2025

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. 

Let us discuss it with an example to get how internally the work is getting executed. Here we will be using the knowledge gathered above as follows:

Example 1: Setting and Getting Thread Priorities

Java
// Java Program to Illustrate Priorities in Multithreading
// via help of getPriority() and setPriority() method
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());
    }

    // Main driver method
    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();

        // Thread - 0, 1 , 2 signify 1 , 2 , 3
        // respectively
    }
}

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:

Note:

Now geeks you must be wondering out what if we do assign the same priorities to threads then what will happen. All the processing in order to look after threads is carried with help of the thread scheduler. One can refer to the below example of what will happen if the priorities are set to the same and later onwards we will discuss it as an output explanation to have a better understanding conceptually and practically.

Example 2: Threads with the Same Priority

Java
// Java program to Demonstrate that a Child thread
// Getting Same Priority as Parent thread
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

        // Print and display priority of current thread
        System.out.println("t1 thread priority: " + t1.getPriority());
    }
}

Output
Main thread priority: 6
t1 thread priority: 6
Explanation:

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4