Java Thread Priority
A thread’s priority is specified with an integer from 1 (the lowest) to 10 (the highest). Constants Thread.MIN_PRIORITY and Thread.MAX_PRIORITY can also be used. By default, the setPriority() method sets the thread priority to 5, which is the Thread.NORM_PRIORITY. We can use the getPriority() method to check the priority of a given thread object.
Setting priorities may not always have the desired effect because prioritization schemes may be implemented differently on different platforms. However, if we cannot resist messing with priorities, use higher priorities for threads that frequently block (sleeping or waiting for I/O). We may use medium to low-priority for CPU-intensive threads to avoid hogging the processor down.
The program below and its output illustrate the effect of assigning priorities to different threads.
ThreadPriority.java
class A extends Thread {
public void run() {
try {
System.out.println("Thread A started");
for (int i = 1; i <= 3; i++) {
System.out.println("Thread A: Value of i = " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e) { }
System.out.println("End of Thread A");
}
}
class B extends Thread {
public void run() {
try {
System.out.println("Thread B started");
for (int j = 1; j <= 3; j++) {
System.out.println("Thread B: Value of j = " + j);
Thread.sleep(500);
}
}
catch (InterruptedException e) { }
System.out.println("End of Thread B");
}
}
class C extends Thread {
public void run() {
try {
System.out.println("Thread C started");
for (int k = 1; k <= 3; k++) {
System.out.println("Thread C: Value of k = " + k);
Thread.sleep(500);
}
}
catch (InterruptedException e) { }
System.out.println("End of Thread C");
}
}
public class ThreadPriority {
public static void main(String s[]) {
A a = new A();
B b = new B();
C c = new C();
c.setPriority(Thread.MAX_PRIORITY); //priority of thread C is 10
b.setPriority(b.getPriority() + 1); //priority of thread B is 6
a.setPriority(Thread.MIN_PRIORITY); //priority of thread A is 1
System.out.println("Start Thread A");
a.start();
System.out.println("Start Thread B");
b.start();
System.out.println("Start Thread C");
c.start();
}
}
Output:
Start Thread A
Start Thread B
Start Thread C
Thread B started
Thread A started
Thread C started
Thread B: Value of j = 1
Thread C: Value of k = 1
Thread A: Value of i = 1
Thread C: Value of k = 2
Thread B: Value of j = 2
Thread A: Value of i = 2
Thread B: Value of j = 3
Thread A: Value of i = 3
Thread C: Value of k = 3
End of Thread C
End of Thread B
End of Thread A
Explanation: The program output illustrates the effect of assigning higher priority to a thread. It is to be noted that although the thread A started first, the higher priority thread B has preempted it and started printing the output first. Immediately, the thread C that has been assigned the highest priority takes control over the other two threads. The important point is that the highest priority thread always preempts any lower priority threads. The thread A is the last to complete.
NOTE: Generally higher priority threads can be expected to be given preference by the thread scheduler over lower priority threads. However, the implementation of thread scheduling is left up to the JVM implementation.