What is Thread Priority in java?

We have seen the Multithreading and the Lifecycle of the Thread. Each Thread has different priorities. Thread priority in java is an integer range between 1 to 10. The larger the value higher the priority of the Thread.

Using this priority value thread scheduler choose which thread will be executed next.

Thread class has the following three priority static constants.

  • Thread.MIN_PRIORITY = 1
  • Thread.NORM_PRIORITY = 5 (Default)
  • Thread.MAX_PRIORITY = 10

Every Thread created has a Default priority. We can change the priority of the thread to minimum or maximum.

Let’s check the following program to set and get the different thread priorities.

Java Code
class Test {
public static void main(String args[]) throws InterruptedException {
MyThread myThread1 = new MyThread();

MyThread myThread2 = new MyThread();
myThread2.setPriority(3);

MyThread myThread3 = new MyThread();
myThread3.setPriority(9);

System.out.println(“Priority of myThread1 (default): ” + myThread1.getPriority());
System.out.println(“Priority of myThread2: ” + myThread2.getPriority());
System.out.println(“Priority of myThread3: ” + myThread3.getPriority());
}
}

class MyThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.print(” ” + i);
}
System.out.println();
}
}
Output:
Priority of myThread1 (default): 5
Priority of myThread2: 3
Priority of myThread3: 9

If we try to add the priority < 1 or >10 then a runtime error java.lang.IllegalArgumentException will be thrown.

  • The default priority for the main thread is 5, for all remaining threads default priority will be inherited from parent to child. That is whatever priority the parent Thread has the same priority will be there for the child Thread.
  • The highest priority will be executed first.
  • Some platforms do not support the thread priority even though the main() method has priority 5 and the child thread has priority 10, we can’t guarantee that the child Thread will be executed first and then the main Thread.

Getting and Setting the name of the Thread:

  • Every thread in Java has some name by default generated by JVM. We can also provide a customized name to the Thread.
  • We can get and set the name to the Thread by using the following two methods as shown in the program.
Java Code
class Test {
public static void main(String args[]) throws InterruptedException {
System.out.println(“Default name for Main Thread: ” + Thread.currentThread().getName());

MyThread myThread = new MyThread();
System.out.println(“Default name for MyThread: ” + myThread.getName());

myThread.setName(“ABCD”);
System.out.println(“New name: ” + myThread.getName());
}
}

class MyThread extends Thread {
public void run() {}
}

Output:
Default name for Main Thread: main
Default name for MyThread: Thread-0
New name: ABCD

We can also set the name of main thread to different by using Thread.currentThread().setName(“NewName”);

Note Some following points regarding Thread priority.

Thread starvation:

  • This happens when there is insufficient CPU capacity to execute a thread, in the case of low-priority threads or threads that are demoted in favor of other threads.

How Thread Schedular is related to thread priority?

  • Thread Scheduler decides the order of the thread that will execute next and allocates the CPU time. High-priority threads will be executed first than low-priority threads.

Child thread inherits the parent Priority.

  • If we did not set the priority for the child thread then the child thread will always have the same priority as the child thread. 
  • Let’s check the example below
Java Code
class Test {
public static void main(String args[]) throws InterruptedException {

MyThread parentThread = new MyThread();
parentThread.setPriority(7);
System.out.println(“Parent Thread priority: ” + parentThread.getPriority());

parentThread.start();
}
}

class MyThread extends Thread {
public void run() {
// Since this thread is created when parentThread is running, this thread will be child of parentThread
MyThread childThread = new MyThread();
System.out.println(“Child Thread Priority: ” + childThread.getPriority());
}
}
Output:
Parent Thread priority: 7
Child Thread Priority: 7

-A blog by Shwetali Khambe

Related Posts