Thread Priority in Java with Example

Thread priority in Java is a number assigned to a thread that is used by Thread scheduler to decide which thread should be allowed to execute.

In other words, thread priority is a feature in Java that helps the thread scheduler to determine the order in which threads are executed.

Each thread is assigned a different priority in Java that will decide the order (preference) in which it is scheduled for running.

Thread priorities are represented by a number from 1 to 10 that specifies the relative priority of one thread to another. The thread with the highest priority is selected by the scheduler to be executed first.

Default Thread Priority


By default, every Java thread is assigned a priority of Thread.NORM_PRIORITY, which is generally a value of 5. It is the default priority value assigned to a thread.

If you do not explicitly set a priority of thread, it automatically inherits the priority of the main thread that created it. The default priority value 5 is the middle value in the priority range from 1 to 10.

Thread Priority Constants


Thread class in Java provides three priority constants to define the priority of a thread. These are:

  • Thread.MIN_PRIORITY = 1
  • Thread.NORM_PRIORITY = 5
  • Thread.MAX_PRIORITY = 10

The Thread.MIN_PRIORITY is a constant in Java that represents the minimum priority level for a thread. Its value is 1. Similarly, the constant Thread.NORM_PRIORITY represents the normal priority level for a thread. Its value is 5.


The constant Thread.MAX_PRIORITY represents the maximum priority for a thread. Its value is 10. All these priority constants are public, final, and static members of the Thread class.

Equal Priority and Thread Scheduling


In all the previous tutorials, threads we have discussed, are of the same priority (equal priority). The threads of the same priority are provided equal time by Java scheduler.

Thread scheduler selects the thread for execution on the first-come, first-serve basis. That is, the threads having equal priorities share the processor time on the first-come, first-serve basis.

When multiple threads are ready for execution, the highest priority thread is selected and executed by JVM. In case when a high priority thread stops, yields, or enters into the blocked state, a low priority thread starts executing.

If any high priority thread enters into the runnable state, it will preempt the currently running thread forcing it to move to the runnable state. Note that the highest priority thread always preempts any lower priority thread.

How to Get Priority of Current Thread in Java?


Thread class provides a method named getPriority() that is used to determine the priority of a thread. It returns the current priority of a thread through which it is called. The general syntax to get the priority of the current thread is as follows:

ThreadName.getPriority();

Here, ThreadName represents the object reference variable of the Thread class.


Let’s take an example program in which we will determine the priority and name of the current thread.

Example 1:

public class A implements Runnable {
public void run()
{
  System.out.println(Thread.currentThread()); // This method is static.
}
public static void main(String[] args) 
{
   A a = new A();
   Thread t = new Thread(a, "NewThread");
 
   System.out.println("Priority of Thread: " +t.getPriority());
   System.out.println("Name of Thread: " +t.getName());
   t.start();
  }
}
Output:
      Priority of Thread: 5
      Name of Thread: NewThread
      Thread[NewThread,5,main]

In this example, we have created a new thread named “NewThread”. The t.getPriority() method prints the priority of the current thread t. By default, if the thread priority is not explicitly set, it will print the default priority value 5. The t.getName() method prints the name of the thread.

Here, it will display “NewThread” because that is the name given to the thread when we have created it. The t.start() method starts the thread t. The thread moves from the “new” state to the “runnable” state, and it will execute the run() method of the Runnable object ‘a’ in this new thread.

How to Set Priority of Thread in Java?


The setPriority() of Thread class is used to set the priority of a thread. This method accepts an integer value as an argument and sets that value as priority of a thread through which it is called. The syntax to set the priority of a thread is as follows:

ThreadName.setPriority(n); // Here, n is an integer value which ranges from 1 to 10.

Let’s create a Java program in which we will set the priority of a thread. Look at the program code.


Example 2:

public class A implements Runnable {
public void run()
{
  System.out.println(Thread.currentThread()); // This method is static.
}
public static void main(String[] args) 
{
   A a = new A();
   Thread t = new Thread(a, "NewThread");
   t.setPriority(2); // Setting the priority of thread.
 
   System.out.println("Priority of Thread: " +t.getPriority());
   System.out.println("Name of Thread: " +t.getName());
   t.start();
  }
}
Output:
      Priority of Thread: 2
      Name of Thread: NewThread
      Thread[NewThread,2,main]

In this example program, the setPriority() method sets the priority of Thread t to 2. Java will throw an exception named IllegalArgumentException if you set the priority value outside the valid range from 1 to 10. For example:

Thread t = new Thread();
t.setPriority(15); // It will throw IllegalArgumentException.

Thread Priority with Multiple Threads


It has been already discussed that thread priority plays an important role during the process of thread scheduling. The thread scheduler chooses that thread for execution that has the highest priority.

Let’s take an example program where we will implement this concept and will see the output.

Example 3:

public class A implements Runnable {
public void run()
{
  System.out.println(Thread.currentThread()); // This method is static.
}
public static void main(String[] args) 
{
   A a = new A();
   Thread t1 = new Thread(a, "First Thread");
   Thread t2 = new Thread(a, "Second Thread");
   Thread t3 = new Thread(a, "Third Thread");
 
   t1.setPriority(4); // Setting priority of the first thread.
   t2.setPriority(2); // Setting priority of the second thread.
   t3.setPriority(8); // Setting priority of the third thread.
 
   t1.start();
   t2.start();
   t3.start();
  }
}
Output:
       Thread[Third Thread,8,main]
       Thread[First Thread,4,main]
       Thread[Second Thread,2,main]

The priority of Thread t1 is 4, t2 is 2, and t3 is 8. Thread t3 is the highest priority as compared to t1 and t2. But it is not necessary that you will get the same priorities when you will run multiple times.

Example 4:

public class X implements Runnable {
public void run()
{
   System.out.println("Thread X started");
   for(int i = 1; i<=4; i++)
   {
      System.out.println("Thread X: " +i);	  
   }
   System.out.println("Exit from X");
 }
}
public class Y implements Runnable {
public void run()
{
   System.out.println("Thread Y started");	
   for(int j = 0; j <= 4; j++)
   {
     System.out.println("Thread Y: " +j);	 
   }
   System.out.println("Exit from Y");
 }
}
public class Z implements Runnable {
public void run()
{
   System.out.println("Thread Z started");	
   for(int k = 0; k <= 4; k++)
   {
     System.out.println("Thread Z: " +k);	 
   }
   System.out.println("Exit from Z");
 }
}
public class ThreadPriority {
public static void main(String[] args) 
{
   X x = new X();
   Y y = new Y();
   Z z = new Z();
 
   Thread t1 = new Thread(x);
   Thread t2 = new Thread(y);
   Thread t3 = new Thread(z);

   t1.setPriority(Thread.MAX_PRIORITY);
   t2.setPriority(t2.getPriority() + 4);
   t3.setPriority(Thread.MIN_PRIORITY);

   t1.start();
   t2.start();
   t3.start();
 }
}
Output:
       Thread X started
       Thread Z started
       Thread X: 1
       Thread Z: 0
       Thread Y started
       Thread Z: 1
       Thread X: 2
       Thread X: 3
       Thread X: 4
       Exit from X
       Thread Z: 2
       Thread Z: 3
       Thread Y: 0
       Thread Y: 1
       Thread Y: 2
       Thread Y: 3
       Thread Y: 4
       Exit from Y
       Thread Z: 4
       Exit from Z

In a multithreaded program, Java thread priority is basically used to set different priorities to threads so that some threads are given preference over other threads as per your requirements. Higher the priority to a thread, the higher is the chance for thread execution.

Thread class provides three constants MAX_PRIORITY, MIN_PRIORITY, and NORM_PRIORITY that are public, final, and static. I hope that you will have understood how to get and set priority of thread in Java through various example programs. Stay tuned with the next where you will understand how to stop thread in Java through examples.
Thanks for reading!!!