What is Thread Priority?
We know that A process have more than one thread to complete a process.
If a process have more than two thread then we need a order of execution.
Thread priority is a process to maintain the “order of thread” in a process which is based on its importance called “thread priority”.
Types of Thread Priority
There are two types of Thread Priority.
- Preemptive
- Non-Preemptive
1. Preemptive Thread Priority:
When a scheduler schedule the priority of Thread on the basis of JVM is called Preemptive Thread Priority.
In the Preemptive Thread Priority user don’t maintain the order of thread. It is decided by the only JVM scheduler.
2. Non-Preemptive Thread Priority:
In the Non-Preemptive Thread Priority, Thread execution order is decided by the User using thread setPriority() method.
Thread Class provided three constant to define thread priority.
- public static int MIN_PRIORITY
- public static int NORM_PRIORITY
- public static int MAX_PRIORITY
All three constant of Thread Priority range between 1 to 10.
MIN_PRIORITY : The value of MIN_PRIORITY is 1.
NORM_PRIORITY : The value of NORM_PRIORITY is 5.
MAX_PRIORITY : The value of MAX_PRIORITY is 10.
By Default a thread have NORM_PRIORITY means 5.
Example of Thread Priority:
import java.io.*;
import java.lang.Thread;
class Priority extends Thread
{
public void run()
{
System.out.println("Name of
Current thread is:"+Thread.currentThread().getName());
System.out.println("Priority
of Current Threadis:"+Thread.currentThread().getPriority());
}
public static void main(String args[]){
Priority p1=new Priority();
Priority p2=new Priority();
p1.setPriority(Thread.MIN_PRIORITY);
p2.setPriority(Thread.MAX_PRIORITY);
p1.start();
p2.start();
}
}
|
OUTPUT :