Thread priority in java

Each thread has a priority and that helps the thread scheduler to decide which thread should start first. Whenever we create a thread it automatically inherits the priority from the parent thread.  In this post, we will learn what is thread priority in java and how we can set and get the priority of the thread.

Here is the table content of the article will we will cover this topic.
1. What is thread priority in java?
2. Importance of thread priority
3. How to set the priority and get the priority?

Thread priority in java

What is thread priority in java?

As we know each program has at least one thread to run a program. Each thread has a priority. The thread priority is nothing, it just a number assigned to each thread by JVM or user. Whenever we create a thread it inherits the priority from its parent thread. But we can also assign the priority to the thread by setPriority() method.

1. A thread can have priority in range 1 to 10. There are 3 static variables defined in Thread class for priority.

public static final int MIN_PRIORITY = 1;
public static final int NORM_PRIORITY = 5;
public static final int MAX_PRIORITY = 10;

2. The JVM assigns NORM_PRIORITY to the main thread by default. When a Java thread is created from the main thread, it inherits its priority from the main thread.

3. We can modify the priority of thread after creation by use of setPriority() method.

4. The higher integer value means that it has a higher priority. In real-time we give the higher priority to the thread if it is critical thread and need to execute first.

5. The thread scheduler pick first those threads have a higher priority from the RUNNABLE queue. If two threads of the same priority are waiting for the CPU, the scheduler chooses one of them to run in a round-robin fashion

Importance of thread priority

In multithreading, each thread has a priority that helps the thread scheduler to determine the order of execution. The thread scheduler decides if a thread has a higher priority, then it should be run first than lower priority threads.

Let’s say we have three threads thread1, thread2, and thread3 and they have priority 5, 6, and 10 respectively. If all the three threads are in runnable state and waiting for the CPU cycle. Then there is more chance thread3 get the CPU first because its priority is high. But it always not true because the thread scheduler decides the execution of threads based on some criteria.

How to set the priority and get the priority?

The default priority of thread is 5. You can set the priority of a thread is in the range of 1 to 10. There are 3 static variables defined in Thread class for priority.

  1. public static int MIN_PRIORITY
  2. public static int NORM_PRIORITY
  3. public static int MAX_PRIORITY

1. public static int MIN_PRIORITY:  This is the minimum priority value that can hold a thread. The value for this is 1.
2. public static int NORM_PRIORITY: This is the default priority of a thread that assigned by JVM. I do not explicitly define it. The value for this is 5.
3. public static int MAX_PRIORITY: This is the maximum priority of a thread. The value for this is 10.

We can check the priority of the thread and modify it. Thread class provides two methods for priority.

1. public final int getPriority(): This method returns the priority of the thread. It is a public and final method. Its return type is int.

2. public final void setPriority(int newPriority): This method is used to set the priority of a thread. It is a public and final method. Its return type is void it means it doesn’t return anything. newPriority: This method takes only one parameter of int type. A programmer can assign new priority to the thread by providing integer value in the parameter. The integer value must be between 1 to 10. If the value of the parameter doesn’t lie between the 1 to 10 it will throw IllegalArgumentException.

public class ExampleOfPriority extends Thread 
{

	public static void main(String[] args)
        {
		ExampleOfPriority thread1 = new ExampleOfPriority(); 
		ExampleOfPriority thread2 = new ExampleOfPriority(); 
		ExampleOfPriority thread3 = new ExampleOfPriority(); 
  
      System.out.println("The priority of thread1 = " + thread1.getPriority()); // Default 5 
      System.out.println("The priority of thread2 = " + thread2.getPriority()); // Default 5 
      System.out.println("The priority of thread3 = " + thread3.getPriority()); // Default 5 
        
        thread1.setPriority(6); 
        thread2.setPriority(7); 
        thread3.setPriority(8); 
       
        System.out.println("The priority of thread1 = " + thread1.getPriority()); 
        System.out.println("The priority of thread2 = " + thread2.getPriority());  
        System.out.println("The priority of thread3 = " + thread3.getPriority());  
  
        // Main thread 
        System.out.println("The priority of Main thread = " + 
        Thread.currentThread().getPriority()); 
  
        // Main thread priority is set to 10 
        Thread.currentThread().setPriority(10); 
        
        // Main thread 
        System.out.println("The priority of Main thread = " + 
       Thread.currentThread().getPriority()); 

	}
}

Output:
The priority of thread1 = 5
The priority of thread2 = 5
The priority of thread3 = 5
The priority of thread1 = 6
The priority of thread2 = 7
The priority of thread3 = 8
The priority of Main thread = 5
The priority of Main thread = 10

Leave a Comment