Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

thread
setpriority
java programming
communitycreator

What is Thread.setPriority() in Java?

Fouzia Bashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The Thread.setPriority() method is used to change the priority of the thread.

First, the checkAccess() method is called implicitly with no arguments, to check whether this thread has permission to modify the resources or not. If this thread does not have modification access, it results in a SecurityException exception.

Note: checkAccess() is called implicitly by the setPriority() method, not explicitly.

Priorities

There are three properties in the Thread class related to priority:

  1. MAX_PRIORITY: The maximum value is 10, kown as the maximum priority of a thread.
  2. NORM_PRIORITY: The normal value is 5, known as the normal priority of a thread.
  3. MIN_PRIORITY: The minimum value is 1, known as the minimum priority of a thread.

Note: The user-defined priority will be between 1 and 10.

Syntax


final void setPriority(int newPriority)

Parameters

It takes a single integer value as an argument.

  • newPriority: Priority to set this thread.

Return value

The void method does not return any value.

Exceptions

It throws the following exceptions:

  1. IllegalArgumentException: If the priority is out of range MIN_PRIORITY to MAX_PRIORITY.

  2. SecurityException: If this thread has no access to modify its resources.

Code

public class setPriorityExample extends Thread {
// run() method to start it's execution
public void run() {
// Printing about this thread
System.out.println("Priority of "+ Thread.currentThread().getName()
+ " is: " + Thread.currentThread().getPriority());
}
public static void main(String args[]) {
// Creating 4 threads
setPriorityExample thread1= new setPriorityExample();
setPriorityExample thread2= new setPriorityExample();
setPriorityExample thread3= new setPriorityExample();
setPriorityExample thread4= new setPriorityExample();
// Set Priority & name of thread1 Thread
thread1.setPriority(Thread.MAX_PRIORITY);
thread1.setName("Thread-1");
// Set Priority & name of thread2 Thread
thread2.setPriority(Thread.MIN_PRIORITY);
thread2.setName("Thread-2");
// Set Priority & name of thread3 Thread
thread3.setPriority(Thread.NORM_PRIORITY);
thread3.setName("Thread-3");
// Set Priority & name of userdefined Thread thread4
thread4.setPriority(6);
thread4.setName("Thread-4");
// Calling the run() method to start execution
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
Demo code for setPriority()

Explanation

  • Line 5: We print the current thread name and its priority value in the run() method.
  • Lines 10 to 13: We create four threads as the setPriorityExample class is inherited from the Thread class.
  • Line 15: We set thread1 priority to Max_priority, which is 10.
  • Line 18: We set thread2 priority to MIN_priority, which is 1.
  • Line 21: We label thread3 as a normal thread, which means it has no priority. The priority value is 5.
  • Line 24: We label thread4 as a user-defined thread, which means the priority of the thread will be set by the user. In this case, the priority value is 6.
  • Lines 27 to 30: We call the start() method to invoke JVMJava Virtual Machine for thread execution.

IllegalArgumentException

public class setPriorityExample extends Thread {
// run() method to start it's execution
public void run() {
// Printing about this thread
System.out.println("Priority of "+ Thread.currentThread().getName()
+ " is: " + Thread.currentThread().getPriority());
}
public static void main(String args[]) {
// Creating 4 threads
setPriorityExample thread= new setPriorityExample();
// Set Priority & name of thread Thread
thread.setPriority(15);
thread.setName("Thread-1");
thread.start();
}
}
Getting illegal argument exception from the code

Explanation

The above code generates an error IllegalArgumentException because we try to set priority value = 15 in line 13, which is greater than the MAX_PRIORITY value.

RELATED TAGS

thread
setpriority
java programming
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring