What is Thread.setPriority() in Java?
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 thesetPriority()method, not explicitly.
Priorities
There are three properties in the Thread class related to priority:
MAX_PRIORITY: The maximum value is 10, kown as the maximum priority of a thread.NORM_PRIORITY: The normal value is 5, known as the normal priority of a thread.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:
-
IllegalArgumentException: If the priority is out of rangeMIN_PRIORITYtoMAX_PRIORITY. -
SecurityException: If this thread has no access to modify its resources.
Code
public class setPriorityExample extends Thread {// run() method to start it's executionpublic void run() {// Printing about this threadSystem.out.println("Priority of "+ Thread.currentThread().getName()+ " is: " + Thread.currentThread().getPriority());}public static void main(String args[]) {// Creating 4 threadssetPriorityExample thread1= new setPriorityExample();setPriorityExample thread2= new setPriorityExample();setPriorityExample thread3= new setPriorityExample();setPriorityExample thread4= new setPriorityExample();// Set Priority & name of thread1 Threadthread1.setPriority(Thread.MAX_PRIORITY);thread1.setName("Thread-1");// Set Priority & name of thread2 Threadthread2.setPriority(Thread.MIN_PRIORITY);thread2.setName("Thread-2");// Set Priority & name of thread3 Threadthread3.setPriority(Thread.NORM_PRIORITY);thread3.setName("Thread-3");// Set Priority & name of userdefined Thread thread4thread4.setPriority(6);thread4.setName("Thread-4");// Calling the run() method to start executionthread1.start();thread2.start();thread3.start();thread4.start();}}
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
setPriorityExampleclass is inherited from theThreadclass. - Line 15: We set
thread1priority toMax_priority, which is10. - Line 18: We set
thread2priority toMIN_priority, which is1. - Line 21: We label
thread3as a normal thread, which means it has no priority. The priority value is5. - Line 24: We label
thread4as a user-defined thread, which means the priority of the thread will be set by the user. In this case, the priority value is6. - Lines 27 to 30: We call the
start()method to invoke for thread execution.JVM Java Virtual Machine
IllegalArgumentException
public class setPriorityExample extends Thread {// run() method to start it's executionpublic void run() {// Printing about this threadSystem.out.println("Priority of "+ Thread.currentThread().getName()+ " is: " + Thread.currentThread().getPriority());}public static void main(String args[]) {// Creating 4 threadssetPriorityExample thread= new setPriorityExample();// Set Priority & name of thread Threadthread.setPriority(15);thread.setName("Thread-1");thread.start();}}
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.