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.
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.
final void setPriority(int newPriority)
It takes a single integer value as an argument.
newPriority
: Priority to set this thread.The void
method does not return any value.
It throws the following exceptions:
IllegalArgumentException
: If the priority is out of range MIN_PRIORITY
to MAX_PRIORITY
.
SecurityException
: If this thread has no access to modify its resources.
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();}}
run()
method.setPriorityExample
class is inherited from the Thread
class.thread1
priority to Max_priority
, which is 10
.thread2
priority to MIN_priority
, which is 1
.thread3
as a normal thread, which means it has no priority. The priority value is 5
.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
.start()
method to invoke 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();}}
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.