Java Thread Priority
Java allows you to set a priority to threads using setPriority()
. Priority helps the thread scheduler decide which thread to run first (but it’s not guaranteed).
Java में आप setPriority()
का उपयोग करके थ्रेड की प्राथमिकता सेट कर सकते हैं। यह थ्रेड scheduler को तय करने में मदद करता है कि कौन सा थ्रेड पहले चलेगा (लेकिन यह निश्चित नहीं होता)।
Example: Thread Priority
class MyThread extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName() + " running with priority " + Thread.currentThread().getPriority());
}
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
t1.setPriority(Thread.MIN_PRIORITY); // 1
t2.setPriority(Thread.NORM_PRIORITY); // 5
t3.setPriority(Thread.MAX_PRIORITY); // 10
t1.setName("Low Priority Thread");
t2.setName("Normal Priority Thread");
t3.setName("High Priority Thread");
t1.start();
t2.start();
t3.start();
}
}
Low Priority Thread running with priority 1
Normal Priority Thread running with priority 5
High Priority Thread running with priority 10
Normal Priority Thread running with priority 5
High Priority Thread running with priority 10
This example shows how to set thread priorities using constants like MIN_PRIORITY
, NORM_PRIORITY
, and MAX_PRIORITY
.
यह उदाहरण दिखाता है कि कैसे MIN_PRIORITY
, NORM_PRIORITY
, और MAX_PRIORITY
के ज़रिए थ्रेड की प्राथमिकता सेट की जाती है।