In Java, each thread has a priority that is used by the thread scheduler to determine execution order. The priority can be set from 1 to 10, where 10 is the highest priority. When multiple threads have the same priority, the scheduler will select one to execute based on its algorithm. Methods like getPriority() and setPriority() allow retrieving and setting a thread's priority at runtime. Common priority levels defined as constants include MAX_PRIORITY, MIN_PRIORITY, and NORM_PRIORITY. However, the actual effect of priority depends on the underlying operating system.
Java Thread Priority - Set, Get Priority & Constants
1. Thread Priority in Java
CPU
Thread
with
priority 2
Thread
with
priority 5
Thread
with
priority 9
Thread
with
priority 7
Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
2. Thread Priority
ï±Thread Priority is a number assigned to every thread, on which thread
scheduler takes the decision, which thread executes first amongst multiple
threads.
ï±Whenever the thread-scheduler pick a new thread, from the ready thread, it
picks the highest-priority thread.
ï±We can set the priority of each thread .
Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
3. Java Thread Priority
ï±In Java each thread inherits its priority by its
parent thread.
ï±Every thread have priority by default i.e. 5.
ï±We can increase or decrease the thread priority by
built-in method of Thread class of Java.
ï±We can set the priority of thread from 1 to 10.
Where 10 is the highest priority and 1 is the least
priority.
Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
4. Method for the Priority
âą getPriority()
â This method is used to know the priority of a
thread.
e.g.
System.out.println(threadName.getPriority())
Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
5. Method for the Priority
âą setPriority()
â It is used to set the priority from 1 to 10 to a
particular thread.
â e.g.
threadName.setPriority(4);
Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
6. Java Constant for Priority
Following are the in built constant for priorities:
Constant Priority
âą MAX_PRIORITY 10
âą MIN_PRIORITY 1
âą NORM_PRIORITY 5
e.g. for setting the priority using constant:
threadName.setPriority(Thread.MAX_PRIORITY);
threadName.setPriority(Thread.MIN_PRIORITY);
Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
7. Limitation of Java Thread Priority
âą Rule of thread priority is system dependent (OS)
âą Some OS have very few priority level than Java i.e.
10. In this case there is no use to set the high
priority upto 10.
âą When there is more than 1 thread of same
priority then scheduler of OS pick anyone as per
the scheduling algorithm.
Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s
8. public class CheckRunnable {
public static void main(String args[])
{
AThread1 obj1 = new AThread1();
BThread1 obj2 = new BThread1();
CThread1 obj3 = new CThread1();
Thread.currentThread().setPriority(8);
Thread Athread = new Thread(obj1);
Thread Bthread = new Thread(obj2);
Thread Cthread = new Thread(obj3);
System.out.println(Athread.getPriority());
System.out.println(Bthread.getPriority());
System.out.println(Cthread.getPriority());
System.out.println(Thread.currentThread().getPriority());
Bthread.setPriority(Thread.MIN_PRIORITY);
System.out.println(Bthread.getPriority());
Athread.start();
Bthread.start();
Cthread.start();
}
}
class AThread1 implements Runnable
{
public void run()
{
for(int i = 1;i<=5;i++)
{
System.out.println("Athread" + " " + i);
try {Thread.sleep(500);
}catch(Exception e)
{}}}}
class BThread1 implements Runnable
{
public void run()
{
System.out.println("BThread");
}}
class CThread1 implements Runnable
{
public void run()
{
System.out.println("CThread");
}
}
Program to set priority of current thread/parent thread and use of constant
Video link for this topic : https://www.youtube.com/watch?v=xopkQL5tpb4&t=693s