Thread Priority in Java Multi-threading

Hello everyone, in today’s tutorial I will brief you guys about thread priority in Java Multi-threading.

Java assigns to each thread priority in Java Multi-threading and it determines the precedence of the threads with respect to all the other threads running in the system. Further, thread priorities are integers that provide the preference among the threads. A thread’s priority provides weight-age to each of the thread which helps in context switching, that assists in determining when to make a transition from one running thread to the next. The rules for context switching to occur are simple:

  • A thread can give up the control deliberately in multi-threading. After the examination of the threads, the one with the higher priority is provided with the CPU.
  • Pre-empting a lower priority thread by a higher priority thread. Here, the higher priority thread pre-empts a lower priority thread, when the lower priority thread doesn’t yield. The pre-emption takes places no matter what the lower-priority thread is doing.

setPriority( ) method sets the priority of the thread, and this method is a member of Thread.

This form: final void setPriority(int level)

Password Validation Program in Java

The ‘level’ specifies the new priority of the thread. MIN_PRIORITY(1) and MAX_PRIORITY(10) is the range in which value of the ‘level’ exists. NORM_PRIORITY returns the thread to its default priority, which is currently 5.

getPriority( ) method of Thread, is provides the current priority of the thread.

As shown:  Thread.getPriority( )

Example:

package prioritymulti;

class PriorityMulti extends Thread{  
 public void run(){  
   System.out.println("Current Thread name is :"+Thread.currentThread().getName());  
   System.out.println("Current Thread name is:"+Thread.currentThread().getPriority());  
  
  }  
 public static void main(String args[]){  
  PriorityMulti th1=new PriorityMulti();  
  PriorityMulti th2=new PriorityMulti(); 
  PriorityMulti th3=new PriorityMulti(); 
  PriorityMulti th4=new PriorityMulti(); 
  
  System.out.println("Thread priority of th1: " + th1.getPriority()); 
  System.out.println("Thread priority of th2: " + th2.getPriority()); 
  System.out.println("Thread priority of th3: " + th3.getPriority());  
  System.out.println("Thread priority of th4: " + th4.getPriority());  

            
  th1.setPriority(Thread.MIN_PRIORITY);  
  th2.setPriority(Thread.MAX_PRIORITY);  
  th3.setPriority(Thread.NORM_PRIORITY);
  th4.setPriority(6);
  
  
  th1.start();  
  th2.start();  
  th3.start();
  th4.start();
  
  
 }
}

In the example above, run() prints the name along with the priority of the threads when it started. Initially we define 4 threads, and get their priority, which is by default 5, and then set the priority of the threads to the one required (1 to 10). Once the threads are started it would run based on the priorities.

Output:

Thank you everyone, I hope this tutorial was helpful.

Leave a Reply

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