Java Thread Priority in Multithreading Tutorial

In this section, we will learn what the thread priority in multithreading is and how to use it in Java.

What is Thread Priority in Java?

The thread priority is a number that we can associate with each thread. These numbers are ranged from 1 to 10, where the number one means the lowest priority and the number 10 means the highest priority.

These priority-numbers give a hint to the underlying scheduler to get priority to threads with higher threads compared to those that have lower priorities.

Note that these values are just a hint for the underlying scheduler. That means it’s up to the scheduler to decide whether to give more priority to those threads with higher priority or not. So we should not depend on these values when designing a multithreaded program in Java.

Also, note that by default, a thread inherits the priority of the thread that creates it. For example, when creating a thread using the `Main` thread of a program, that sub-thread gets the priority of the `main` thread.

Types of Priority in Java:

You can put a number from 1 to 10 as the priority of a thread. Also, the Thread class has 3 constants that represent a level of priority for a thread:

  • MIN_PRIORITY
  • NORM_PRIORITY
  • MAX_PRIORITY

public static int MIN_PRIORITY

This constant represents the value 1, which is the lowest level of priority a thread can get.

public static int NORM_PRIORITY

This constant represents the value 5, which is the mid-level of priority for a thread to have.

public static int MAX_PRIORITY

This constant represents the value 10, which is the highest level of priority that could be assigned to a thread.

How to get and set the priority of a Thread?

In Java, in order to set or get the priority of a thread, we use the setPriority() and getPriority() methods, respectively.

Thread getPriority() Method

The getPriority() method is used to get the priority of a thread.

The method does not take an argument, and calling it would return a number between 1 to 10, depending on the priority of the target thread.

Example: using getPriority method

public class Main {
    
    public static void main(String[] args) {
        Thread t1 = new Thread(Main::run, "Thread-One");
        t1.start();
        
    }
    public static void run(){
        System.out.println("The priority of the thread: "+Thread.currentThread().getName()+" is: "+Thread.currentThread().getPriority());
    }

}

Output:

The priority of the thread: Thread-One is: 5

Here, the priority of the `t1` thread is 5. This value is inherited from the main thread of the program.

Thread setPriority() Method

The Java Thread setPriority() method is used to set the priority of a thread in Java.

The value we set for this method could be either the values between 1 to 10 or the three constants of the Thread class that we mentioned at the beginning of this article.

Note that the method is final and does not return a value.

Example: using setPriority Method

public class Main {

    public static void main(String[] args) {
        Thread t1 = new Thread(Main::run, "Thread-One");
        t1.setPriority(Thread.MAX_PRIORITY);
        t1.start();


    }
    public static void run(){
        System.out.println("The priority of the thread: "+Thread.currentThread().getName()+" is: "+Thread.currentThread().getPriority());
    }

}

Output:

The priority of the thread: Thread-One is: 10

We changed the priority of this thread from 5 (Its default value) to 10 using the `setPriority()` method and the value of the constant Thread.MAX_PRIORITY

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies