Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Java

Java में थ्रेड प्रायोरिटी का महत्व?


मल्टी-थ्रेडेड एप्लिकेशन में, प्रत्येक थ्रेड को एक प्रायोरिटी असाइन की जाती है। प्रोसेसर को थ्रेड शेड्यूलर द्वारा थ्रेड की प्रायोरिटी के आधार पर असाइन किया जाता है यानी सबसे अधिक प्रायोरिटी वाले थ्रेड को प्रोसेसर पहले असाइन किया जाता है और इसी तरह। थ्रेड की डिफ़ॉल्ट प्रायोरिटी मान '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
  1. Java 9 में destroyForcibly() मेथड का महत्व Java 9 में destroyForcibly() मेथड का महत्व

    Java 9 ने Process क्लास में destroyForcibly() मेथड पेश किया जो प्रक्रियाओं को बलपूर्वक समाप्त करने का एक विश्वसनीय तरीका प्रदान करता है जब सामान्य destroy() (विनम्र समाप्ति) विफल हो जाता है या बहुत धीमा होता है। destroy() बनाम destroyForcibly() पहलूdestroy()destroyForcibly() सिग्नलSIGTERM

  1. Java में SwingWorker क्लास का महत्व क्या है Java में SwingWorker क्लास का महत्व क्या है

    एक SwingWorker क्लास हमें एक वर्कर थ्रेड में एक असिंक्रोनस टास्क (जैसे लंबे समय तक चलने वाला टास्क) करने में सक्षम बनाती है फिर टास्क परिणामों के आधार पर इवेंट डिस्पैच थ्रेड (EDT) से Swing कंपोनेंट्स को अपडेट करती है। इसे Java 1.6 संस्करण में पेश किया गया था।SwingWorker क्लासjava.swing.SwingWorker क

  1. Java में SwingUtilities क्लास का महत्व क्या है? Java में SwingUtilities क्लास का महत्व क्या है?

    Java में, स्विंग कंपोनेंट्स स्क्रीन पर प्रदर्शित होने के बाद, केवल एक थ्रेड द्वारा संचालित किए जा सकते हैं जिसे इवेंट हैंडलिंग थ्रेड कहा जाता है। हम अपना कोड एक अलग ब्लॉक में लिख सकते हैं और इस ब्लॉक का रेफरेंस इवेंट हैंडलिंग थ्रेड को दे सकते हैं। SwingUtilities क्लास में दो महत्वपूर्ण स्टेटिक मेथड्