Thread Priority in Java with Example
Thread priority in Java is a number assigned to a thread that is used by Thread scheduler to decide which thread should be allowed to execute.
In Java, each thread is assigned a different priority that will decide the order (preference) in which it is scheduled for running.
Thread priorities are represented by a number from 1 to 10 that specifies the relative priority of one thread to another. The thread with the highest priority is selected by the scheduler to be executed first.
The default priority of a thread is 5. Thread class in Java also provides several priority constants to define the priority of a thread. These are:
1. MIN_PRIORITY = 1
2. NORM_PRIORITY = 5
3. MAX_PRIORTY = 10
These constants are public, final, and static members of the Thread class.
In all the previous tutorials, threads we have discussed, are of the same priority (equal priority). The threads of the same priority are provided equal time by Java scheduler.
Thread scheduler selects the thread for execution on the first-come, first-serve basis. That is, the threads having equal priorities share the processor time on the first-come, first-serve basis.
When multiple threads are ready for execution, the highest priority thread is selected and executed by JVM. In case when a high priority thread stops, yields, or enters into the blocked state, a low priority thread starts executing.
If any high priority thread enters into the runnable state, it will preempt the currently running thread forcing it to move to the runnable state. Note that the highest priority thread always preempts any lower priority thread.
How to Get Priority of Current Thread in Java?
Thread class provides a method named getPriority() that is used to determine the priority of a thread. It returns the priority of a thread through which it is called.
[adinserter block=”5″]
Let’s create a Java program in which we will determine the priority and name of the current thread.
Program code 1:
public class A implements Runnable { public void run() { System.out.println(Thread.currentThread()); // This method is static. } public static void main(String[] args) { A a = new A(); Thread t = new Thread(a, "NewThread"); System.out.println("Priority of Thread: " +t.getPriority()); System.out.println("Name of Thread: " +t.getName()); t.start(); } }
Output: Priority of Thread: 5 Name of Thread: NewThread Thread[NewThread,5,main]
In the preceding program, the getPriority() method is called using reference variable t of Thread class, hence it returns the priority of current thread through Thread t.
How to Set Priority of Thread in Java?
The setPriority() of Thread class is used to set the priority of a thread. This method accepts an integer value as an argument and sets that value as priority of a thread through which it is called. The syntax to set the priority of a thread is as follows:
[adinserter block=”2″]
ThreadName.setPriority(n); // Here, n is an integer value which ranges from 1 to 10.
Let’s create a Java program in which we will set the priority of a thread. Look at the program source code.
Program code 2:
public class A implements Runnable { public void run() { System.out.println(Thread.currentThread()); // This method is static. } public static void main(String[] args) { A a = new A(); Thread t = new Thread(a, "NewThread"); t.setPriority(2); // Setting the priority of thread. System.out.println("Priority of Thread: " +t.getPriority()); System.out.println("Name of Thread: " +t.getName()); t.start(); } }
Output: Priority of Thread: 2 Name of Thread: NewThread Thread[NewThread,2,main]
In this example program, the setPriority() method sets the priority of Thread t to 2.
It has been already discussed that thread priority plays an important role during the process of thread scheduling. The thread scheduler chooses that thread for execution that has the highest priority.
Let’s take an example program where we will implement this concept and will see the output.
Program code 3:
public class A implements Runnable { public void run() { System.out.println(Thread.currentThread()); // This method is static. } public static void main(String[] args) { A a = new A(); Thread t1 = new Thread(a, "First Thread"); Thread t2 = new Thread(a, "Second Thread"); Thread t3 = new Thread(a, "Third Thread"); t1.setPriority(4); // Setting priority of the first thread. t2.setPriority(2); // Setting priority of the second thread. t3.setPriority(8); // Setting priority of the third thread. t1.start(); t2.start(); t3.start(); } }
Output: Thread[Third Thread,8,main] Thread[First Thread,4,main] Thread[Second Thread,2,main]
The priority of Thread t1 is 4, t2 is 2, and t3 is 8. Thread t3 is the highest priority as compared to t1 and t2. But it is not necessary that you will get the same priorities when you will run multiple times.
Program code 4:
public class X implements Runnable { public void run() { System.out.println("Thread X started"); for(int i = 1; i<=4; i++) { System.out.println("Thread X: " +i); } System.out.println("Exit from X"); } } public class Y implements Runnable { public void run() { System.out.println("Thread Y started"); for(int j = 0; j <= 4; j++) { System.out.println("Thread Y: " +j); } System.out.println("Exit from Y"); } } public class Z implements Runnable { public void run() { System.out.println("Thread Z started"); for(int k = 0; k <= 4; k++) { System.out.println("Thread Z: " +k); } System.out.println("Exit from Z"); } } public class ThreadPriority { public static void main(String[] args) { X x = new X(); Y y = new Y(); Z z = new Z(); Thread t1 = new Thread(x); Thread t2 = new Thread(y); Thread t3 = new Thread(z); t1.setPriority(Thread.MAX_PRIORITY); t2.setPriority(t2.getPriority() + 4); t3.setPriority(Thread.MIN_PRIORITY); t1.start(); t2.start(); t3.start(); } }
Output: Thread X started Thread Z started Thread X: 1 Thread Z: 0 Thread Y started Thread Z: 1 Thread X: 2 Thread X: 3 Thread X: 4 Exit from X Thread Z: 2 Thread Z: 3 Thread Y: 0 Thread Y: 1 Thread Y: 2 Thread Y: 3 Thread Y: 4 Exit from Y Thread Z: 4 Exit from Z
In a multithreaded program, Java thread priority is basically used to set different priorities to threads so that some threads are given preference over other threads as per our requirements. Higher the priority to a thread, the higher is the chance for thread execution. Thread class provides three constants MAX_PRIORITY, MIN_PRIORITY, and NORM_PRIORITY that are public, final, and static.
We hope that you will have understood how to get and set priority of thread in Java through various example programs. In the next, we will understand how to stop thread in Java through examples.
Thanks for reading!!!