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.
- public static final int MIN_PRIORITY = 1
- public static final int NORM_PRIORITY = 5
- public static final int MAX_PRIORITY = 10
Two methods exist in Thread class to set the priority and get the existing priority.
- int getPriority() : To retrieve the priority.
- 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
public void run()
for(int i=0; i<10; i++)
System.out.println(this.getName() + ": " + i);
} // you can use join() method also
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.
Notes on Thread Priority setPriority getPriority methods
With the following two statements, priorities are given to threads.
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.
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.