জাভাতে Advanced Locking Techniques এবং Custom Locks ব্যবহার করে মাল্টিথ্রেডিং প্রোগ্রামিং আরও কার্যকর এবং নমনীয় করা যায়। এটি ডেটা সিঙ্ক্রোনাইজেশন উন্নত করে এবং রেস কন্ডিশন এড়াতে সাহায্য করে।
Advanced Locking Techniques
জাভার java.util.concurrent.locks প্যাকেজে বিভিন্ন ধরনের লকিং টেকনিক পাওয়া যায়। এগুলো মাল্টিথ্রেডেড পরিবেশে সিঙ্ক্রোনাইজেশনের উন্নত পদ্ধতি প্রদান করে।
১. ReentrantLock
ReentrantLock একটি উন্নত লক, যা থ্রেডকে একই লক বারবার ধরে রাখতে দেয়, যদি এটি আগেই লক করে থাকে। এটি ঐচ্ছিক টাইমআউট এবং ইন্টারাপশন সমর্থন করে।
ReentrantLock উদাহরণ:
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockExample {
private final ReentrantLock lock = new ReentrantLock();
private int counter = 0;
public void increment() {
lock.lock();
try {
counter++;
System.out.println(Thread.currentThread().getName() + " incremented counter to: " + counter);
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
ReentrantLockExample example = new ReentrantLockExample();
Runnable task = example::increment;
Thread t1 = new Thread(task, "Thread-1");
Thread t2 = new Thread(task, "Thread-2");
t1.start();
t2.start();
}
}
২. ReadWriteLock
ReadWriteLock একসঙ্গে একাধিক থ্রেডকে পড়ার অনুমতি দেয়, কিন্তু লেখার জন্য শুধুমাত্র এক থ্রেডকে অনুমতি দেয়। এটি রিড-হেভি অ্যাপ্লিকেশনের জন্য উপযুক্ত।
ReadWriteLock উদাহরণ:
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockExample {
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private int value = 0;
public int readValue() {
lock.readLock().lock();
try {
return value;
} finally {
lock.readLock().unlock();
}
}
public void writeValue(int newValue) {
lock.writeLock().lock();
try {
value = newValue;
System.out.println("Value updated to: " + value);
} finally {
lock.writeLock().unlock();
}
}
public static void main(String[] args) {
ReadWriteLockExample example = new ReadWriteLockExample();
Thread reader = new Thread(() -> {
System.out.println("Read Value: " + example.readValue());
});
Thread writer = new Thread(() -> example.writeValue(42));
reader.start();
writer.start();
}
}
৩. StampedLock
StampedLock একটি উন্নত লকিং টেকনিক, যা optimistic locking সমর্থন করে। এটি ReadWriteLock এর তুলনায় দ্রুততর।
StampedLock উদাহরণ:
import java.util.concurrent.locks.StampedLock;
public class StampedLockExample {
private final StampedLock lock = new StampedLock();
private int value = 0;
public int readValue() {
long stamp = lock.tryOptimisticRead();
int currentValue = value;
if (!lock.validate(stamp)) { // চেক করুন লক বৈধ কিনা
stamp = lock.readLock();
try {
currentValue = value;
} finally {
lock.unlockRead(stamp);
}
}
return currentValue;
}
public void writeValue(int newValue) {
long stamp = lock.writeLock();
try {
value = newValue;
System.out.println("Value updated to: " + value);
} finally {
lock.unlockWrite(stamp);
}
}
public static void main(String[] args) {
StampedLockExample example = new StampedLockExample();
Thread reader = new Thread(() -> {
System.out.println("Read Value: " + example.readValue());
});
Thread writer = new Thread(() -> example.writeValue(42));
reader.start();
writer.start();
}
}
Custom Locks তৈরি করা
কাস্টম লক তৈরি করতে Lock ইন্টারফেস ব্যবহার করে নিজস্ব লকিং মেকানিজম বাস্তবায়ন করা যায়।
Custom Lock উদাহরণ: SimpleLock
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.Condition;
class SimpleLock implements Lock {
private boolean isLocked = false;
@Override
public synchronized void lock() {
while (isLocked) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
isLocked = true;
}
@Override
public synchronized void unlock() {
isLocked = false;
notify();
}
@Override
public void lockInterruptibly() throws InterruptedException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean tryLock() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean tryLock(long time, java.util.concurrent.TimeUnit unit) throws InterruptedException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Condition newCondition() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
public class CustomLockExample {
private final SimpleLock lock = new SimpleLock();
private int counter = 0;
public void increment() {
lock.lock();
try {
counter++;
System.out.println(Thread.currentThread().getName() + " incremented counter to: " + counter);
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
CustomLockExample example = new CustomLockExample();
Runnable task = example::increment;
Thread t1 = new Thread(task, "Thread-1");
Thread t2 = new Thread(task, "Thread-2");
t1.start();
t2.start();
}
}
Custom ReentrantLock উদাহরণ
class CustomReentrantLock {
private boolean isLocked = false;
private Thread lockingThread = null;
private int holdCount = 0;
public synchronized void lock() {
Thread currentThread = Thread.currentThread();
while (isLocked && currentThread != lockingThread) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
isLocked = true;
lockingThread = currentThread;
holdCount++;
}
public synchronized void unlock() {
if (Thread.currentThread() == lockingThread) {
holdCount--;
if (holdCount == 0) {
isLocked = false;
lockingThread = null;
notify();
}
}
}
}
Custom Locks এর সুবিধা
- নিয়ন্ত্রণ বৃদ্ধি: লকিং মেকানিজমে কাস্টমাইজেশন।
- কনটেক্সট-স্পেসিফিক লক: নির্দিষ্ট সমস্যা সমাধানে ফোকাসড।
- Advanced Locking Techniques যেমন
ReentrantLock,ReadWriteLock, এবংStampedLockউন্নত কনকারেন্সি সমাধান প্রদান করে। - Custom Locks তৈরি করে নির্দিষ্ট সমস্যার জন্য বিশেষ লকিং মেকানিজম তৈরি করা যায়।
- সঠিক লকিং প্রযুক্তি ব্যবহার করে ডেটা সিঙ্ক্রোনাইজেশন উন্নত করা এবং রেস কন্ডিশন এড়ানো সম্ভব।
Read more