Thread Priority in Java


Java Priority ThreadViews 2845

Thread Priority in Java

In a Java multithreading environment, every thread has a priority which is an integer value between 1 and 10 where 1 is the lowest and 10 is the highest. Thread priority is an essential concept since the scheduler picks up the threads for execution based on the priority. The thread scheduler gives preference to higher priority threads. This means it executes the high priority threads first and then the low priority threads.  It also depends on the JVM implementation. If there are multiple threads with different priorities, it uses preemptive scheduling based on priority. Suppose if threads have the same priorities, then it may use the FCFS concept or waiting time.

Java thread priority in Multithreading

Java thread Priority in Multithreading

Constant variables defining Thread priority

The Thread class has 3 constant variables that define the thread priorities. We can assign priorities for the user threads else the system automatically assigns default values.

  • MIN_PRIORITY: This is the minimum priority with a value of 1.
    public static int MIN_PRIORITY
  • NORM_PRIORITY: This is the default normal priority with a value of 5.
    public static int NORM_PRIORITY
  • MAX_PRIORITY: This is the maximum priority with a value of 10.
    public static int MAX_PRIORITY

Getter and setter methods for Thread priorities

We can get and set the priority values for threads in Java using the getPriority() and setPriority() methods. Based on these priorities, the thread scheduler decides the order of thread execution. We should also remember that there is no guarantee that the scheduler always executes the high priority threads first since it also depends on the JVM implementation.

public final int getPriority()

The getPriority() method retrieves the current priority value for a thread. It returns an integer value that represents the thread priority.

Example:

In the below example, we can see that system assigns default values to all the threads, which is 5. Using the getPriority() method, we can retrieve the thread priority value. Since all the threads have the same priority, the scheduler may either use the FCFS concept or use the waiting time value to decide the order of thread execution.

public class ThreadPriorityDemo extends Thread{
  
  public void run() {
    System.out.println("Thread " + Thread.currentThread().getName() + " running");
  }

  public static void main(String[] args) {
    
    ThreadPriorityDemo th1 = new ThreadPriorityDemo();
    ThreadPriorityDemo th2 = new ThreadPriorityDemo();
    ThreadPriorityDemo th3 = new ThreadPriorityDemo();
    
    th1.start();
    th2.start();
    th3.start();
    
    System.out.println("Priority of thread " + th1.getName() + " : " + th1.getPriority());
    System.out.println("Priority of thread " + th2.getName() + " : " + th2.getPriority());
    System.out.println("Priority of thread " + th3.getName() + " : " + th3.getPriority());
    
  }

}
Priority of thread Thread-0 : 5
Priority of thread Thread-1 : 5
Priority of thread Thread-2 : 5
Thread Thread-0 running
Thread Thread-1 running
Thread Thread-2 running

public final void setPriority(int newPriority)

The setPriority() method allows the user to set new priority values for a thread. It accepts an integer value as a parameter that sets the new priority value to the thread. When we do not set priority for a thread, the system automatically sets some priority value for threads.

Example:

The below example shows how we can set the priority for threads using constant variables like MIN_PRIORITY, NORM_PRIORITY, MAX_PRIORITY. The thread that has maximum priority will be initiated first, else it depends on the JVM implementation as well. In this example, we can see that though the threads were started in a different order, it executes the thread with priority 10 first, followed by priority 5, and then finally with priority 1.

public class setThreadPriority extends Thread {
  
  public void run() {
    System.out.println("Thread " + Thread.currentThread().getName() + " started");
    System.out.println("Thread with priority " + Thread.currentThread().getPriority() + " is running");
  }

  public static void main(String[] args) {
    setThreadPriority t1 = new setThreadPriority();
    setThreadPriority t2 = new setThreadPriority();
    setThreadPriority t3 = new setThreadPriority();
    
    t1.setPriority(MIN_PRIORITY);
    t2.setPriority(NORM_PRIORITY);
    t3.setPriority(MAX_PRIORITY);
    
    t1.start();
    t2.start();
    t3.start();

  }

}
Thread Thread-2 started
Thread with priority 10 is running
Thread Thread-0 started
Thread Thread-1 started
Thread with priority 5 is running
Thread with priority 1 is running

We can also set user-defined integer values for setting the thread priority in Java. The below example illustrates the same. We can set any integer value between 1 and 10 as thread priority. Hence the scheduler executes the thread with priority 9 first, followed by 3, and then finally 1.

public class setThreadPriority extends Thread {
  
  public void run() {
    System.out.println("Thread " + Thread.currentThread().getName() + " started");
    System.out.println("Thread with priority " + Thread.currentThread().getPriority() + " is running");
  }

  public static void main(String[] args) {
    setThreadPriority t1 = new setThreadPriority();
    setThreadPriority t2 = new setThreadPriority();
    setThreadPriority t3 = new setThreadPriority();
    
    t1.setPriority(3);
    t2.setPriority(9);
    t3.setPriority(1);
    
    t1.start();
    t2.start();
    t3.start();

  }

}
Thread Thread-0 started
Thread Thread-1 started
Thread Thread-2 started
Thread with priority 9 is running
Thread with priority 3 is running
Thread with priority 1 is running

Suppose, we try to assign any integer value greater than 10 or less than 1, we will get an exception as below.

public class setThreadPriority extends Thread {
  
  public void run() {
    System.out.println("Thread " + Thread.currentThread().getName() + " started");
    System.out.println("Thread with priority " + Thread.currentThread().getPriority() + " is running");
  }

  public static void main(String[] args) {
    setThreadPriority t1 = new setThreadPriority();
    setThreadPriority t2 = new setThreadPriority();
    setThreadPriority t3 = new setThreadPriority();
    
    t1.setPriority(3);
    t2.setPriority(9);
    t3.setPriority(15); //This will throw an exception
    
    t1.start();
    t2.start();
    t3.start();

  }

}
Exception in thread "main" java.lang.IllegalArgumentException
  at java.base/java.lang.Thread.setPriority(Thread.java:1137)
  at setThreadPriority.main(setThreadPriority.java:15)

Example: Child thread inherits parent thread priority

When we do not specify any thread priority for a child thread, it always inherits the same priority from the parent thread. In the below example, we can see that both the parent thread(main) and the child threads have the same priorities.

public class mainThreadDemo extends Thread {
  
  public void run() {
    System.out.println("Thread running");
  }

  public static void main(String[] args) {
    Thread.currentThread().setPriority(7);
    System.out.println("Main thread priority: " + Thread.currentThread().getPriority());
    
    mainThreadDemo t = new mainThreadDemo();
    System.out.println("Child thread priority: " + t.getPriority());
    
    t.start();

  }

}
Main thread priority: 7
Child thread priority: 7
Thread running

Conclusion

In this tutorial, we have seen how to get and set the thread priorities in Java using the getPriority and setPriority methods with examples. We have also seen how to use the constant variables that define the thread priorities.

Reference

Translate »