Lesson 42
Thread Priority
Thread priority is a value from 1 to 10 that hints to the JVM scheduler which thread should execute next.
Java Track
Save progress as you learn.
61 lessons
Lesson content
Every Java thread has a priority value. The scheduler uses it to decide which thread runs next.
Definition List
- Thread Priority: A value that influences thread scheduling order.
- Thread Scheduler: JVM component that decides which thread executes.
- CPU Time: Processor time allocated to a thread.
- Yield: Temporarily pauses the current thread for others to execute.
Priority Range in Java
- MIN_PRIORITY — 1
- NORM_PRIORITY — 5 (default)
- MAX_PRIORITY — 10
Thread Priority Constants
public static int MIN_PRIORITY // 1
public static int NORM_PRIORITY // 5
public static int MAX_PRIORITY // 10Setting Thread Priority
// Syntax
thread.setPriority(priorityValue);
// Example
thread.setPriority(Thread.MAX_PRIORITY);Getting Thread Priority
// Syntax
thread.getPriority();
// Example
System.out.println(thread.getPriority());Example 01: Thread Priority in Java
package com.java.Multi_threading;
class PriorityExample {
public static void main(String[] args) {
Thread thread1 = new Thread(new MyRunnable(), "Thread 1");
Thread thread2 = new Thread(new MyRunnable(), "Thread 2");
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " - Count: " + i);
Thread.yield();
}
}
}Output
Thread 2 - Count: 0
Thread 2 - Count: 1
Thread 1 - Count: 0
Thread 2 - Count: 2
Thread 1 - Count: 1
...Thread Priority Methods
- setPriority() — Assigns a priority to a thread.
- getPriority() — Returns the current thread priority.
- currentThread() — Returns a reference to the executing thread.
- getName() — Returns the name of the thread.
Understanding Thread.yield()
Thread.yield();Suggests the current thread pause for others. The scheduler may ignore it.
Best Practices
- Use default priority for most applications.
- Avoid extreme MIN or MAX priority values.
- Don't rely solely on priority for thread control.
- Test scheduling behavior across different platforms.
FAQ
1. What is thread priority in Java?
A value between 1 and 10 that influences CPU time allocation.
2. What is the default thread priority?
Thread.NORM_PRIORITY — value of 5.
3. Does higher priority guarantee execution first?
No. Priority is a hint only. JVM and OS decide final order.
Where Java is used
Java in real-world development
MIN_PRIORITY
Value of 1. Lowest scheduling priority. Use for background tasks.
NORM_PRIORITY
Value of 5. Default priority assigned to every new thread.
MAX_PRIORITY
Value of 10. Highest scheduling priority. Use with care.
setPriority()
Assigns a new priority to a thread. Accepts values 1 through 10.
getPriority()
Returns the current priority value of a thread.
Thread.yield()
Suggests the scheduler pause the current thread. Not guaranteed.