CountDownLatch জাভা কনকারেন্সি ফ্রেমওয়ার্কের একটি টুল, যা একাধিক থ্রেডের মধ্যে Coordination এবং Synchronization নিশ্চিত করার জন্য ব্যবহৃত হয়। এটি একটি কাউন্টার-ভিত্তিক মেকানিজম, যেখানে থ্রেডগুলি নির্দিষ্ট সংখ্যক কার্যক্রম সম্পন্ন হওয়া পর্যন্ত অপেক্ষা করে।
CountDownLatch কী?
- CountDownLatch হলো java.util.concurrent প্যাকেজের একটি ক্লাস।
- এটি একটি কাউন্টার দিয়ে কাজ করে। কাউন্টারটি একটি নির্দিষ্ট মান দিয়ে শুরু হয় এবং প্রতি
countDown()কলের মাধ্যমে কমে। - যখন কাউন্টার শূন্যে পৌঁছায়, তখন অপেক্ষমাণ থ্রেডগুলি কাজ শুরু করতে পারে।
CountDownLatch এর ব্যবহার
- মাল্টিপল থ্রেড একসঙ্গে কাজ শুরু করার আগে নির্দিষ্ট শর্ত পূরণের জন্য অপেক্ষা করা।
- প্রধান থ্রেডকে অন্যান্য থ্রেডের কাজ শেষ হওয়া পর্যন্ত অপেক্ষা করানো।
ক্লাসের প্রধান মেথড
CountDownLatch(int count): নির্দিষ্ট কাউন্টার দিয়ে একটি CountDownLatch তৈরি করে।void countDown(): কাউন্টার ১ করে কমায়।void await(): কাউন্টার শূন্য না হওয়া পর্যন্ত থ্রেডকে অপেক্ষা করায়।
CountDownLatch উদাহরণ: Multiple Thread Coordination
১. একাধিক থ্রেড কাজ শুরু করার আগে অপেক্ষা করা
import java.util.concurrent.CountDownLatch;
public class CountDownLatchExample {
public static void main(String[] args) {
int numberOfWorkers = 3;
CountDownLatch latch = new CountDownLatch(1); // একবার কাউন্টডাউন করলে থ্রেডগুলো শুরু হবে
// কাজের থ্রেড তৈরি
for (int i = 1; i <= numberOfWorkers; i++) {
new Thread(new Worker(latch), "Worker-" + i).start();
}
System.out.println("Main thread is preparing the workers...");
try {
Thread.sleep(2000); // মেইন থ্রেড কিছু প্রস্তুতি নিচ্ছে
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main thread is starting the workers.");
latch.countDown(); // সব থ্রেড কাজ শুরু করতে পারে
}
}
class Worker implements Runnable {
private final CountDownLatch latch;
public Worker(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
try {
latch.await(); // অপেক্ষা করুন যতক্ষণ না কাউন্টডাউন হয়
System.out.println(Thread.currentThread().getName() + " is working.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
আউটপুট:
Main thread is preparing the workers...
Main thread is starting the workers.
Worker-1 is working.
Worker-2 is working.
Worker-3 is working.
২. প্রধান থ্রেডকে অপেক্ষা করানো যতক্ষণ না কাজ শেষ হয়
import java.util.concurrent.CountDownLatch;
public class CountDownLatchCompletionExample {
public static void main(String[] args) {
int numberOfTasks = 3;
CountDownLatch latch = new CountDownLatch(numberOfTasks);
// কাজের থ্রেড তৈরি
for (int i = 1; i <= numberOfTasks; i++) {
new Thread(new Task(latch), "Task-" + i).start();
}
try {
latch.await(); // মেইন থ্রেড অপেক্ষা করবে যতক্ষণ না কাজ শেষ হয়
System.out.println("All tasks are completed. Main thread can proceed.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Task implements Runnable {
private final CountDownLatch latch;
public Task(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " is starting.");
try {
Thread.sleep(1000); // কাজ করছে
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " has finished.");
latch.countDown(); // কাজ শেষ হয়েছে, কাউন্টডাউন করুন
}
}
আউটপুট:
Task-1 is starting.
Task-2 is starting.
Task-3 is starting.
Task-1 has finished.
Task-2 has finished.
Task-3 has finished.
All tasks are completed. Main thread can proceed.
CountDownLatch ব্যবহার: প্রযোজক-ভোক্তা (Producer-Consumer) উদাহরণ
import java.util.concurrent.CountDownLatch;
public class ProducerConsumerExample {
public static void main(String[] args) {
CountDownLatch latch = new CountDownLatch(1);
Thread producer = new Thread(() -> {
try {
System.out.println("Producer is producing items...");
Thread.sleep(2000); // আইটেম প্রোডিউস হচ্ছে
System.out.println("Producer has finished producing items.");
latch.countDown(); // প্রোডিউসার কাজ শেষ করেছে
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread consumer = new Thread(() -> {
try {
System.out.println("Consumer is waiting for items...");
latch.await(); // কনজিউমার অপেক্ষা করছে
System.out.println("Consumer has started consuming items.");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
producer.start();
consumer.start();
}
}
আউটপুট:
Consumer is waiting for items...
Producer is producing items...
Producer has finished producing items.
Consumer has started consuming items.
CountDownLatch এর সুবিধা
- Multiple Thread Coordination: একাধিক থ্রেডের মধ্যে কার্যকর সমন্বয় নিশ্চিত করে।
- Concurrency Management: মেইন থ্রেড বা অন্য থ্রেডগুলোকে কাজ শেষ হওয়া পর্যন্ত অপেক্ষা করানো সহজ হয়।
- সিম্পল ডিজাইন: সরল API ব্যবহার করে শক্তিশালী কনকারেন্সি সমাধান।
CountDownLatch এর সীমাবদ্ধতা
- One-Time Use: একবার কাউন্ট শূন্যে পৌঁছে গেলে, এটি পুনরায় ব্যবহার করা যায় না। নতুন কাউন্টার দিয়ে নতুন CountDownLatch তৈরি করতে হবে।
- ডেডলক সম্ভাবনা: যদি কোনো থ্রেড ভুলভাবে
countDown()না করে, তবে সিস্টেম ডেডলকে যেতে পারে।
CountDownLatch একটি শক্তিশালী কনকারেন্সি টুল, যা মাল্টিপল থ্রেড কোঅর্ডিনেশনে সহজ এবং কার্যকর সমাধান প্রদান করে। এটি বিশেষত এমন পরিস্থিতিতে উপযোগী যেখানে নির্দিষ্ট কাজ শেষ না হওয়া পর্যন্ত অন্য কাজ শুরু করা যাবে না। সঠিক ব্যবহার নিশ্চিত করতে কোডের লজিক পরিষ্কার রাখা গুরুত্বপূর্ণ।
Content added By
Read more