public static int MIN_PRIORITY = 1
→ Lowest prioritypublic static int NORM_PRIORITY = 5
→ Default prioritypublic static int MAX_PRIORITY = 10
→ Highest priority1
to 10
.
But it cannot be less than 1
and cannot be greater than 10
.
public final void setPriority(int newPriority)
1
(MIN_PRIORITY) and 10
(MAX_PRIORITY).
start()
, but should be set before the thread starts running.
public final int getPriority()
class MyThread extends Thread
{
@Override
public void run()
{
System.out.println(getName() + " is running with priority: " + getPriority());
}
}
public class ThreadPriorityDemo
{
public static void main(String[] args)
{
MyThread low = new MyThread();
MyThread high = new MyThread();
low.setName("Low-Priority-Thread");
high.setName("High-Priority-Thread");
// Set priorities
low.setPriority(Thread.MIN_PRIORITY); // 1
high.setPriority(Thread.MAX_PRIORITY); // 10
// Start low first, then high
low.start();
high.start();
}
}
High-Priority-Thread is running with priority: 10 Low-Priority-Thread is running with priority: 1
Note: Although the higher priority thread often gets preference, the output is not guaranteed because thread scheduling depends on the JVM and operating system.
1
(lowest) and 10
(highest).5
(normal priority).
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.