मल्टी-थ्रेडेड एप्लिकेशन में, प्रत्येक थ्रेड को एक प्रायोरिटी असाइन की जाती है। प्रोसेसर को थ्रेड शेड्यूलर द्वारा थ्रेड की प्रायोरिटी के आधार पर असाइन किया जाता है यानी सबसे अधिक प्रायोरिटी वाले थ्रेड को प्रोसेसर पहले असाइन किया जाता है और इसी तरह। थ्रेड की डिफ़ॉल्ट प्रायोरिटी मान '5' के साथ होती है। हम Thread क्लास के getPriority() मेथड का उपयोग करके थ्रेड की प्रायोरिटी प्राप्त कर सकते हैं।
Thread क्लास में थ्रेड की प्रायोरिटी के लिए परिभाषित तीन स्टैटिक मान हैं:
MAX_PRIORITY
यह 10 मान के साथ अधिकतम थ्रेड प्रायोरिटी है।
NORM_PRIORITY
यह 5 मान के साथ डिफ़ॉल्ट थ्रेड प्रायोरिटी है।
MIN_PRIORITY
यह 1 मान के साथ न्यूनतम थ्रेड प्रायोरिटी है।
सिंटैक्स
public final int getPriority()
उदाहरण
public class ThreadPriorityTest extends Thread { public static void main(String[]args) { ThreadPriorityTest thread1 = new ThreadPriorityTest(); ThreadPriorityTest thread2 = new ThreadPriorityTest(); ThreadPriorityTest thread3 = new ThreadPriorityTest(); System.out.println("Default thread priority of thread1: " + thread1.getPriority()); System.out.println("Default thread priority of thread2: " + thread2.getPriority()); System.out.println("Default thread priority of thread3: " + thread3.getPriority()); thread1.setPriority(8); thread2.setPriority(3); thread3.setPriority(6); System.out.println("New thread priority of thread1: " + thread1.getPriority()); System.out.println("New thread priority of thread2: " + thread2.getPriority()); System.out.println("New thread priority of thread3: " + thread3.getPriority()); } }आउटपुट
Default thread priority of thread1: 5 Default thread priority of thread2: 5 Default thread priority of thread3: 5 New thread priority of thread1: 8 New thread priority of thread2: 3 New thread priority of thread3: 6