Priority Management হল জাভার মাল্টি-থ্রেডিংয়ের একটি গুরুত্বপূর্ণ অংশ। এটি থ্রেডগুলোকে প্রায়োরিটি বা অগ্রাধিকার নির্ধারণ করতে সক্ষম করে, যা নির্দেশ করে কোন থ্রেড আগে প্রসেসর সময় পাবে। জাভায় থ্রেড প্রায়োরিটি ব্যবস্থাপনা Thread.setPriority() মেথডের মাধ্যমে করা হয়।
থ্রেড প্রায়োরিটি (Thread Priority)
- প্রতিটি থ্রেডের একটি প্রায়োরিটি লেভেল থাকে, যা ১ থেকে ১০ এর মধ্যে নির্ধারিত।
- Default Priority:
Thread.NORM_PRIORITY(মানে, ৫)। - থ্রেড প্রায়োরিটি তিনটি কনস্ট্যান্টের মাধ্যমে পরিচালিত হয়:
Thread.MIN_PRIORITY= 1 (সর্বনিম্ন প্রায়োরিটি)Thread.NORM_PRIORITY= 5 (ডিফল্ট প্রায়োরিটি)Thread.MAX_PRIORITY= 10 (সর্বোচ্চ প্রায়োরিটি)
Thread.setPriority() মেথডের ব্যবহার
Thread.setPriority() মেথড ব্যবহার করে থ্রেডের প্রায়োরিটি নির্ধারণ করা হয়।
সিনট্যাক্স:
public final void setPriority(int newPriority)
newPriority: থ্রেডের নতুন প্রায়োরিটি, যা ১ থেকে ১০ এর মধ্যে হতে হবে। এর বাইরে কোনো মান দিলেIllegalArgumentExceptionছুড়ে দেয়।
কোড উদাহরণ
১. থ্রেড প্রায়োরিটির উদাহরণ
public class ThreadPriorityExample {
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println("Thread 1 is running");
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println("Thread 2 is running");
}
});
// প্রায়োরিটি সেট করা
thread1.setPriority(Thread.MAX_PRIORITY); // সর্বোচ্চ প্রায়োরিটি (10)
thread2.setPriority(Thread.MIN_PRIORITY); // সর্বনিম্ন প্রায়োরিটি (1)
// থ্রেড শুরু করা
thread1.start();
thread2.start();
}
}
আউটপুট (সম্ভাব্য):
Thread 1 is running
Thread 1 is running
Thread 1 is running
Thread 1 is running
Thread 1 is running
Thread 2 is running
Thread 2 is running
Thread 2 is running
Thread 2 is running
Thread 2 is running
ব্যাখ্যা: উচ্চ প্রায়োরিটি থাকা থ্রেড (Thread 1) সাধারণত বেশি প্রসেসর সময় পায়। তবে এটি JVM এর থ্রেড শিডিউলার এবং প্ল্যাটফর্মের উপর নির্ভরশীল।
২. প্রায়োরিটির সাথে একাধিক থ্রেড
public class MultiThreadPriorityExample {
public static void main(String[] args) {
Runnable task = () -> {
String threadName = Thread.currentThread().getName();
int priority = Thread.currentThread().getPriority();
for (int i = 0; i < 3; i++) {
System.out.println(threadName + " with priority " + priority + " is running");
}
};
Thread thread1 = new Thread(task, "Thread-1");
Thread thread2 = new Thread(task, "Thread-2");
Thread thread3 = new Thread(task, "Thread-3");
// প্রায়োরিটি সেট করা
thread1.setPriority(Thread.MIN_PRIORITY); // ১
thread2.setPriority(Thread.NORM_PRIORITY); // ৫ (ডিফল্ট)
thread3.setPriority(Thread.MAX_PRIORITY); // ১০
thread1.start();
thread2.start();
thread3.start();
}
}
আউটপুট (সম্ভাব্য):
Thread-3 with priority 10 is running
Thread-3 with priority 10 is running
Thread-3 with priority 10 is running
Thread-2 with priority 5 is running
Thread-2 with priority 5 is running
Thread-2 with priority 5 is running
Thread-1 with priority 1 is running
Thread-1 with priority 1 is running
Thread-1 with priority 1 is running
ব্যাখ্যা: উচ্চ প্রায়োরিটি থ্রেড (Thread-3) বেশি প্রায়োরিটি পাওয়ায়, এটি আগে এক্সিকিউট হয়। তবে কিছু ক্ষেত্রে এটি নিশ্চিত নয়, কারণ এটি প্ল্যাটফর্ম-নির্ভর।
Priority Management এর চ্যালেঞ্জ
- JVM-নির্ভরতা: থ্রেড প্রায়োরিটি সম্পূর্ণভাবে JVM-এর থ্রেড শিডিউলার এবং অপারেটিং সিস্টেমের উপর নির্ভরশীল।
- অনিশ্চিত পারফরম্যান্স: উচ্চ প্রায়োরিটি থাকা থ্রেড সর্বদা আগে চলবে এমনটি নিশ্চিত নয়।
- ডেডলক: থ্রেডের ভুল প্রায়োরিটি ম্যানেজমেন্ট ডেডলকের সম্ভাবনা বাড়ায়।
Priority Management এর সেরা পদ্ধতি
- ডিফল্ট প্রায়োরিটি ব্যবহার করুন:
Thread.NORM_PRIORITYব্যবহার করা সাধারণত নিরাপদ। - পরিকল্পিত প্রায়োরিটি নির্ধারণ করুন: শুধুমাত্র প্রয়োজন অনুযায়ী প্রায়োরিটি পরিবর্তন করুন।
- Thread Pool ব্যবহার করুন:
ExecutorsAPI এর মাধ্যমে থ্রেড পরিচালনা করতে পারলে প্রায়োরিটি সমস্যা অনেকাংশে এড়ানো যায়।
Thread.setPriority() এর মাধ্যমে জাভায় থ্রেড প্রায়োরিটি সেট করা সম্ভব। এটি থ্রেডগুলোর শিডিউলিং প্রভাবিত করতে পারে, তবে এটি সম্পূর্ণরূপে প্ল্যাটফর্ম এবং JVM এর উপর নির্ভর করে। Priority Management সঠিকভাবে প্রয়োগ করলে মাল্টি-থ্রেডিং কর্মক্ষমতা উন্নত করা সম্ভব।
Read more