Advertisement



< Prev
Next >



Threads Priorities




In Java, each and every thread has a priority which is defined by a number between, 1 to 10, with 1 being the lowest priority, 10 being the highest priority. A default priority for a thread is 5 but you can even set the priority for a thread. Ideally, a thread having a maximum priority(10) should get the maximum CPU time but it's not always the case. Here's why -:

Some JVMs use time slicing scheduling of threads, where each thread gets a particular amount of time to run on CPU before it is replaced by some other thread, whereas some JVMs use pre-emptive or priority based scheduling, in which a thread with the maximum priority gets the maximum amount of time to run on CPU.

Hence, you should never base an important outcome of your program on thread priorities because setting the thread with maximum priority is not a guarantee of maximum CPU time.




How to set priority of a Thread.


To set a thread's priority, we can use setPriority() method of Thread class, which looks like this -:

Method Description
final void setPriority(int priority) Sets the priority of a Thread.

where setPriority() method can be passed an int value between number 1 to 10, or alternatively, we can even pass one of the three static final variables of Thread class -:

By default, each thread gets a default priority of 5, i.e. Thread.NORM_PRIORITY




How to get priority of a Thread.


To get a thread's priority, we can use getPriority() method of Thread class, which looks like this:

Methods Description
final int getPriority()
Returns the priority of a Thread.





Setting and getting the priority of a Thread


Let us set and get the priorities of a Thread by an example in which we are creating a class that implements Runnable interface and implementing its run() method.

// Java - Program to set and get the priority of a thread

class ThreadRun2 implements Runnable
{
//Entry Point of the new thread.
public void run()	
{
	//Getting the reference to this thread.
	Thread t=Thread.currentThread();

	//Getting the default priority of NewThread.
	System.out.println("Default priority of "+ t.getName() + " : " +  t.getPriority());

	//Setting the priority of NewThread to 10.
	t.setPriority(Thread.MAX_PRIORITY); 

	//Getting the name and priority of this thread
	System.out.println("Priority of " + t.getName() + " : " + t.getPriority());  
	}


public static void main(String... ar)
{
	ThreadRun2 newTh = new ThreadRun2();
	Thread th1= new Thread(newTh, "NewThread");//Calling Thread's constructor & passing the object
						   //of class that has implemented  Runnable interface
						   //& the name of new thread.
	
	//starting the new thread, calls run() method automatically
	th1.start(); 
}
}


Output is -:


Default priority of NewThread : 5
Priority of NewThread : 10





Program Analysis





Advertisement




Ideally, a high-priority thread gets more CPU time than a low-priority thread.


Let us see a simple example of how a high-priority thread gets more CPU time than a low-priority thread. Although you must also remember that this may not happen in all the cases, due to the factors which we have discussed at the beginning of this tutorial.

// Java - Program to set and get the priority of a thread

class ThreadRun2 implements Runnable
{
//Entry Point of the new thread.
public void run()	
{
	//Getting the reference to this thread.
	Thread t=Thread.currentThread();


	int total=0;
	for(int i=0;i<100000000; i++)
	{	
		total = total +i;	
	} 
	System.out.println(t.getName() + " with priority " + t.getPriority() + " has finished its execution with total = "+ total);

}


public static void main(String... ar)
{
	//Getting the reference to this thread.
	Thread t =Thread.currentThread();

	
	//Setting the name of the main thread
	t.setName("Main Thread");


	//Setting the priority of Main Thread to minimum priority i.e. 1
	t.setPriority(Thread.MIN_PRIORITY);


	//Creating the object of our class
	ThreadRun2 newTh = new ThreadRun2();


	Thread th1= new Thread(newTh, "NewThread");//Calling Thread's constructor & passing the object
						//of class that has implemented  Runnable interface
						//& the name of new thread.

	
	//Setting the priority of NewThread to maximum priority i.e 10.
	th1.setPriority(Thread.MAX_PRIORITY); 


	//starting the new thread, calls run() method automatically
	th1.start(); 


	int total=0;
	for(int i=0;i<100000000; i++)
	{	
		total = total +i;	
	}

	System.out.println(t.getName() + " with priority " + t.getPriority() + " has finished its execution with total = "+ total);
}
}


Output is -:


NewThread with priority 10 has finished its execution with total = 887459712
Main Thread with priority 1 has finished its execution with total = 887459712


Program Analsis






Please share this article -




< Prev
Next >
< Creating a Thread
join() in Thread>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement