Last Updated: 25 June, 2022
In Java Multithreading, every thread has a priority. Priority is an integer in the range of 1 to 10, where 1 is the lowest priority and 10 is the highest priority. The default priority of a thread is 5 (Normal Priority).
The priority of a thread is assigned by the JVM or by the programmer itself, explicitly through the Java code. The thread priority is used by the thread scheduler to pick up the threads for execution based on the priority.
The thread scheduler first executes the highest priority threads and then the lowest priority threads, but it also depends on the JVM implementation. If there are multiple threads with different priorities, it uses preemptive scheduling based on priority. In a situation where multiple threads have the same priority, it may use the FCFS (First Come, First Server) concept or wait time.
Get and Set priority of a thread in Java:
As given below, the Java Thread class has provided two methods for getting and setting the thread priority.
public final int getPriority(): This method returns the priority of the given thread.
public final void setPriority(int newPriority): This method updates the priority of the thread to newPriority. If we set our of specified range of priority value then this method throws java.lang.IllegalArgumentException.
Example 1 : A sample Java programme for setting and getting Thread Priority
Here, we are creating three threads - t1, t2, and t3.
See the output below.
Output
Thread default priority:
t1 Thread Priority : 5
t2 Thread Priority : 5
t3 Thread Priority : 5
Thread updated priority:
t1 Thread Priority : 3
t2 Thread Priority : 5
t3 Thread Priority : 8
Currently Executing Thread : main
Main thread priority : 5
Main thread priority : 10
Constants defined in Thread class:
There are 3 constants defined as given below.
Example 2 : Setting thread priority using Constants
Output
Thread Thread-0 started with Priority : 1
Thread Thread-2 started with Priority : 10
Thread Thread-1 started with Priority : 5
What if, we assign out of range priority
Using the setPriority() method, if we assign out of range (1-10) priority value then JVM will throw IllegalArgumentException
.
Example 3 : Setting thread priority out of range (1-10)
Output
Exception in thread "main" java.lang.IllegalArgumentException at java.base/java.lang.Thread.setPriority(Thread.java:1141) at com.multithreading.example.ThreadPriorityExample3.main(ThreadPriorityExample3.java:6)
Always remember that if we are assigning the thread priority, the priority value must be 1–10 only if assigning beyond the range will get java.lang.IllegalArgumentException at runtime.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the default priority of a thread?
The default priority of a thread is 5 (NORM_PRIORITY).
What is Thread Scheduler?
The thread scheduler is a JVM component It allocates CPU time to threads. Thread Scheduler determines the execution order of multiple runnable state threads on a single processor (CPU). If multiple threads have different priorities, it uses preemptive scheduling based on priority. In case multiple threads have the same priorities, then it may use the FCFS (First Come First Server) concept or waiting time.
What is Preemptive scheduling?
Preemptive Scheduling is a thread scheduling mechanism where the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.
What is time slicing?
In the thread scheduler, time slicing is used to divide CPU time among the active threads.
What is the difference between preemptive scheduling and time slicing?
In preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. In time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks.
Does JVM accept priority value out of range?
No, If we set out of specified range (1-10) of priority value then JVM will throw java.lang.IllegalArgumentException.