coderz.py

Keep Coding Keep Cheering!

Java Thread Priority

java thread class
What is Thread Priority?

The concept of priorities in threads states that each thread has a priority. In layman’s terms, each object has a priority here, and the priorities are represented by numbers from 1 to 10.

Note:

  • As expected, the default priority setting is set to 5.
  • 1 is the minimum priority.
  • The highest priority is set at 10.
Here, it defines 3 constants, specifically as follows:
  • public static int NORM PRIORITY
  • public static int MIN PRIORITY
  • public static int MAX PRIORITY
How to get and set the priority of a thread in java?
  • public final int getPriority(): The method Thread.getPriority() returns the given thread’s priority.
  • public final void setPriority(int newPriority): This method sets the thread’s priority to the value specified by the argument newPriority. If the value of the parameter newPriority exceeds the minimum(1) and maximum(10) limit, this method throws an IllegalArgumentException.
Example of priority of a Thread:
// Java Program to Illustrate Priorities in Multithreading
// via help of getPriority() and setPriority() method

// Importing required classes
import java.lang.*;

// Main class
public class Coderz extends Thread {

  // Method 1
  // run() method for the thread that is called
  // as soon as start() is invoked for thread in main()
  public void run()
  {
    // Print statement
    System.out.println(" run() method");
  }

  // Main driver method
  public static void main(String[] args)
  {
    // Creating random threads
    // with the help of above class
    Coderz t1 = new Coderz();
    Coderz t2 = new Coderz();
    Coderz t3 = new Coderz();

    // Thread 1
    // Display the priority of above thread
    // using getPriority() method
    System.out.println("t1 thread priority : "
            + t1.getPriority());

    // Thread 1
    // Display the priority of above thread
    System.out.println("t2 thread priority : "
            + t2.getPriority());

    // Thread 3
    System.out.println("t3 thread priority : "
            + t3.getPriority());

    // Setting priorities of above threads by
    // passing integer arguments
    t1.setPriority(8);
    t2.setPriority(1);
    t3.setPriority(3);

    // t3.setPriority(21); will throw
    // IllegalArgumentException

    // 8
    System.out.println("t1 thread priority : "
            + t1.getPriority());

    // 1
    System.out.println("t2 thread priority : "
            + t2.getPriority());

    // 3
    System.out.println("t3 thread priority : "
            + t3.getPriority());

    // Main thread

    // Displays the name of
    // currently executing Thread
    System.out.println(
      "Currently Executing Thread : "
      + Thread.currentThread().getName());

    System.out.println(
      "Main thread priority : "
      + Thread.currentThread().getPriority());

    // Main thread priority is set to 10
    Thread.currentThread().setPriority(10);

    System.out.println(
      "Main thread priority : "
      + Thread.currentThread().getPriority());
  }
}
Output:
t1 thread priority : 5
t2 thread priority : 5
t3 thread priority : 5
t1 thread priority : 8
t2 thread priority : 1
t3 thread priority : 3
Currently Executing Thread : main
Main thread priority : 5
Main thread priority : 10
Example of IllegalArgumentException:
import java.lang.*;  
  
public class IllegalArgumentException extends Thread   
{  
  
// the main method  
public static void main(String argvs[])  
{  
  
// Now, priority of the main thread is set to 11, which is greater than 10  
Thread.currentThread().setPriority(11);  
  
// The current thread is retrieved  
// using the currentThread() method  
  
// displaying the main thread priority  
// using the getPriority() method of the Thread class  
System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority());  
  
}  
}  
Output:
Exception in thread "main" java.lang.IllegalArgumentException
	at java.base/java.lang.Thread.setPriority(Thread.java:1138)
	at IllegalArgumentException.main(IllegalArgumentException.java:11)

Note: also read about the Naming a Thread in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement