Thread Priority setPriority getPriority


Introduction to Thread Priority/h6>

Thread scheduling of waiting threads, to the processor, is done by the thread scheduler. Thread scheduler is part of the OS and different operating systems come with different scheduling algorithms. When some threads are to be executed before the other, a programmer can give priorities to the threads. As per the priorities, the scheduler schedules the waiting threads. The thread with more priority is given the first preference to the processor than the low priority thread.

A thread can be given 10 priorities starting from 1 to 10 (both inclusive). To give the priority, the thread class comes with three symbolic constants, defined in Thread class, as given under.

  1. public static final int MIN_PRIORITY = 1
  2. public static final int NORM_PRIORITY = 5
  3. public static final int MAX_PRIORITY = 10

Two methods exist in Thread class to set the priority and get the existing priority.

  1. int getPriority() : To retrieve the priority.
  2. void setPriority(int) : To give the priority.

The next program uses the constants and methods to write the priorities.

public class HighLess extends Thread
{
  public static void main(String args[])
  {
   HighLess hl1 = new HighLess();
   HighLess hl2 = new HighLess();     
                                                                     // setting priorities       
   hl1.setPriority(Thread.MAX_PRIORITY-1);                           // 9
   hl2.setPriority(Thread.MIN_PRIORITY+1);                           // 3
                                                                     // setting the names
   hl1.setName("High");                                              // for 8  thread 
   hl2.setName("Less");                                              // for 3 thread   
                                                                     // to retrieve the priorities
   System.out.println("High Priority is "  + hl1.getPriority());     // prints 9
   System.out.println("Less Priority is " +  hl2.getPriority());     // prints 3

   hl2.start();                                                      // wantedly hl2 is started first
   hl1.start();    
  }
  public void run()
  {
    for(int i=0; i<10; i++)
    {
      System.out.println(this.getName() + ": "  + i);
    }        
  }
}                                                                    // you can use join() method also

Thread Priority setPriority getPriority

String threadName = Thread.currentThread().getName();

This statement retrieves the name of the running thread. It is in abridged form and can be written with more understandably (with two statements) as follows.

Thread t1 = Thread.currentThread();
String s1 = t1.getName();

currentThread() of Thread class refers the thread running presently (currently) the run() method. Thread object t1 refers either ml1 or ml2. It becomes like ml1.currentThread() internally and returns a thread object which contains the reference of ml1. ml1.getName() returns the name of ml1. This approach (of getting a thread object) is better when more properties of Thread are required like t1.getPriority(), t1.isDaemon() etc.

The thread name can also be obtained as follows (as used in earlier programs).

String s1 = this.getName();

With the following two statements, names are given to threads and these names are used in run() method to distinguish the threads which is executing the run() method.

ml1.setName("Less");
ml2.setName("More");

Notes on Thread Priority setPriority getPriority methods

With the following two statements, priorities are given to threads.

ml1.setPriority(Thread.MIN_PRIORITY+1);
ml2.setPriority(Thread.MAX_PRIORITY-1);

Thread ml1 is given a priority of 2 (1+1) as MIN_PRIORITY is evaluated to 1 at runtime. Similarly, ml2 priority is evaluated to 9 (10-1) as MAX_PRIORITY is evaluated to 10 at runtime.

ml1.start(); ml2.start();

Purposefully, the thread with less priority, ml1, is started first. But still, ml2 starts first that got higher priority. Observe the screenshot.

Pass your comments of this posting of how far it is helpful in understanding the concepts Thread Priority setPriority getPriority methods.

7 thoughts on “Thread Priority setPriority getPriority”

  1. Respected Sir, I have tried threads with 3 priorities 1 5 and 7.At times it happens that thread with priority 1 gets completed first then 7 and then 5 .But according to priority the completion of execution should be in the order 1,5,7 .

  2. Respected Sir,
    I have tried threads with 3 priorities 1 5 and 7.At times it happens that thread with priority 7 gets completed first .Why Sir?

  3. sir the program is not giving the same output as you write means priority is not working as it should works . here it is randomly showing “high” and “less”

Leave a Comment

Your email address will not be published.