Is lesson me hum seekhenge:
- Thread priority kya hota hai
- Priority ka use kyun hota hai
- setPriority() aur getPriority()
- Priority constants
- Real examples aur behavior
Thread priority ka matlab:
thread ko execution ke liye kitni importance di gayi hai
Higher priority:
CPU scheduling me jyada chance milta hai
Java me thread priority range:
1 to 10
| Constant | Value |
|---|---|
| MIN_PRIORITY | 1 |
| NORM_PRIORITY | 5 |
| MAX_PRIORITY | 10 |
har thread ki default priority = 5 (NORM_PRIORITY)
thread.setPriority(value);Example:
t1.setPriority(10); // high priority
t2.setPriority(1); // low prioritySystem.out.println(t1.getPriority());class Test extends Thread {
public void run(){
System.out.println(Thread.currentThread().getName() +
" Priority: " + Thread.currentThread().getPriority());
}
public static void main(String[] args){
Test t1 = new Test();
Test t2 = new Test();
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}
}Thread priority execution guarantee nahi deta
Matlab:
high priority thread pehle run kare ye zaruri nahi
Ye depend karta hai:
OS scheduler
Hospital me emergency patient → high priority
normal patient → low priority
class Test extends Thread {
public void run(){
for(int i = 1; i <= 3; i++){
System.out.println(Thread.currentThread().getName());
}
}
public static void main(String[] args){
Test t1 = new Test();
Test t2 = new Test();
t1.setPriority(8);
t2.setPriority(3);
t1.start();
t2.start();
}
}child thread parent ki priority inherit karta hai
✔ Priority range 1–10 hoti hai
✔ default priority = 5
✔ OS scheduler decide karta hai execution
✔ priority sirf hint hota hai
❌ Priority ko guaranteed execution samajhna
❌ logic ko priority par depend banana
- Thread priority kya hota hai?
- MAX_PRIORITY ki value kya hoti hai?
- Kya priority execution guarantee karta hai?
- Default priority kya hoti hai?
Is lesson me humne seekha:
✔ Thread priority concept
✔ setPriority() aur getPriority()
✔ constants (MIN, MAX, NORM)
✔ real behavior
Thread priority Java me execution preference dene ke liye use hota hai, lekin ye guaranteed nahi hota.