Thread Priorities in Java : Free Java Tutorials and Online Training

Thread priorities decide the order of execution of threads in a multi-threaded environment. Every thread in Java has some priority. It may be default priority generated by JVM or customized priority provided by the programmer.

Thread priority is defined by a number. The valid range of thread priorities is 1 to 10 where 1 is minimum and 10 is the maximum priority. 0 is not a valid number for thread priority.

Thread class defines following constants to represent some standard priorities:

Thread.MIN_PRIORITY = 1

Thread.NORM_PRIORITY = 5

Thread.MAX_PRIORITY = 10

Thread scheduler will use thread priorities while allocating processor time to threads.

If two threads have the exact same priority, then we can not predict exact execution order as it depends on the scheduling algorithm.


Two methods to get and set the thread priorities in Java

Thread class has two following methods:

1) public final int getPriority()

2) public final void setPriority(int p)

Note that while setting the priority of a thread, the priority value should be in the range 1 to 10. If we try to set priority value outside this thread, JVM will give Illegal Argument Exception.

Default priority for main thread is 5. For other threads, default priority will be inherited from parent thread to child thread.

// We create our own class named MyThread that extends Thread class
class MyThread extends Thread
{	
	public void run()
        {
    
        }
}

// Main class
public class Demo
{
	public static void main(String args[])
	{
		// printing priority value of main thread
		System.out.println("main thread priority is " + Thread.currentThread().getPriority());		

		// Creating a thread object of our class named MyThread and starting the thread.
		MyThread thread = new MyThread();
		thread.start();

		// printing priority value of child thread
		System.out.println("child thread priority is " + thread.getPriority());		
		
		// setting child thread priority value to 8
		thread.setPriority(8);
		
		// printing priority value of child thread
		System.out.println("child thread priority is " + thread.getPriority());		
	}
}

Output:

main thread priority is 5
child thread priority is 5
child thread priority is 8

This way we can get and set the priority of threads in Java.


Set child thread priority high

We can set child thread priority higher than main thread by setting priority value to any value greater than 5 and make child thread complete its execution before main thread. Look at the code below for better understanding.

// We create our own class named MyThread that extends Thread class
class MyThread extends Thread
{	
	public void run()
	{
		System.out.println("Child thread execution is finished");				
	}
}

// Main class
public class Demo
{
	public static void main(String args[])
	{
		// Creating a thread object of our class named MyThread and starting the thread.
		MyThread thread = new MyThread();
		// setting child thread priority value to 8
		//thread.setPriority(8);
		thread.start();

		System.out.println("Main thread execution is finished");		
	}
}

Output:

Child thread execution is finished
Main thread execution is finished

However, some platforms won’t provide proper support for thread priorities. In that case, you will not get the same output. You should contact Operating System company and ask for patch file to support setting value for thread priority.

 

Leave a Comment

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