How to set Thread priority in Java


This tutorial will explain the “How to set the priority of Thread in Java”. Every thread has a priority and priority represents the integer number from 1 to 10.

By using method setPriority(int newPriority), we can set the priority of thread and using getPriority() method, we can check the priority of a thread.

Thread class provides 3 constant priorities

  1. MIN_PRIORITY– set minimum priority of thread i.e. 1
  2. NORM_PRIORITY– set normal priority of thread i.e. 5
  3. MAX_PRIORITY– set maximum priority of thread i.e. 10

As we know priority represents the integer number from 1 to 10, we can set the custom or user-defined priority of a thread.

  • CUST_PRIORITY– set a custom priority of a thread.

Define Custom Priority

int CUST_PRIORITY = 7;
setPriority(CUST_PRIORITY);
//or
setPriority(7);

Throws:
IllegalArgumentException if the priority is not in the range MIN_PRIORITY to MAX_PRIORITY and
SecurityException if the current thread cannot modify this thread.

Note: First the checkAccess method of this thread is called with no arguments. Determines if the currently running thread has permission to modify this thread.

Example of Minimum Priority Thread

MinPriorityThreadExp.java
package org.websparrow.thread.methods;

public class MinPriorityThreadExp extends Thread {
	@Override
	public void run() {
		Thread thread = new Thread();
		thread.checkAccess();
		thread.setPriority(MIN_PRIORITY);
		System.out.println("Priority of thread is : " + thread.getPriority());
	}

	public static void main(String[] args) throws IllegalArgumentException, SecurityException {
		MinPriorityThreadExp minPriority = new MinPriorityThreadExp();
		minPriority.start();
	}
}

Output:

Priority of thread is : 1

Example of Normal Priority Thread

NormPriorityThreadExp.java
package org.websparrow.thread.methods;

public class NormPriorityThreadExp extends Thread {
	@Override
	public void run() {
		Thread thread = new Thread();
		thread.checkAccess();
		thread.setPriority(NORM_PRIORITY);
		System.out.println("Priority of thread is : " + thread.getPriority());
	}

	public static void main(String[] args) throws IllegalArgumentException, SecurityException {
		NormPriorityThreadExp normPriority = new NormPriorityThreadExp();
		normPriority.start();
	}
}
Output:
Priority of thread is : 5

Example of Maximum Priority Thread

MaxPriorityThreadExp.java
package org.websparrow.thread.methods;

public class MaxPriorityThreadExp extends Thread {
	@Override
	public void run() {
		Thread thread = new Thread();
		thread.checkAccess();
		thread.setPriority(MAX_PRIORITY);
		System.out.println("Priority of thread is : " + thread.getPriority());
	}

	public static void main(String[] args) throws IllegalArgumentException, SecurityException {
		MaxPriorityThreadExp maxPriority = new MaxPriorityThreadExp();
		maxPriority.start();
	}
}

Output:

Priority of thread is : 10

Example of Custom Priority Thread

CustPriortyThreadExp.java
package org.websparrow.thread.methods;

public class CustPriortyThreadExp extends Thread {
	@Override
	public void run() {
		int CUST_PRIORITY = 7;
		Thread thread = new Thread();
		thread.checkAccess();
		thread.setPriority(CUST_PRIORITY);
		// or you can directly set the priority setPriority(7)
		System.out.println("Priority of thread is : " + thread.getPriority());
	}

	public static void main(String[] args) throws IllegalArgumentException, SecurityException {
		CustPriortyThreadExp custPriority = new CustPriortyThreadExp();
		custPriority.start();
	}
}
Output:
Priority of thread is : 7

Similar Posts

About the Author

Websparrow
I like sharing my experiments and ideas with everyone by writing articles on the latest technological trends.