Compare-And-Swap (CAS) মেকানিজম

Atomic Operations এবং Variables - জাভা কনকারেন্সি (Java Concurrency) - Java Technologies

304

Compare-And-Swap (CAS) হলো একটি লক-ফ্রি কনকারেন্সি কৌশল, যা জাভাতে পারফরম্যান্স উন্নত করার জন্য ব্যবহৃত হয়। এটি atomic operation হিসেবে পরিচিত এবং java.util.concurrent.atomic প্যাকেজে উপলব্ধ।


CAS কীভাবে কাজ করে?

CAS মেকানিজম তিনটি ভ্যালু নিয়ে কাজ করে:

  1. Memory Location (Expected Value): বর্তমান ভ্যালু যা একটি মেমোরি লোকেশনে রয়েছে।
  2. New Value: নতুন ভ্যালু যা আমরা সেট করতে চাই।
  3. Expected Value: বর্তমান ভ্যালু যা আমরা প্রত্যাশা করছি।

কাজের ধাপ:

  • CAS বর্তমান ভ্যালু এবং প্রত্যাশিত ভ্যালু তুলনা করে।
  • যদি তারা মিলে যায়, তবে নতুন ভ্যালু সেট করা হয়।
  • যদি না মিলে যায়, তবে কিছু পরিবর্তন করা হয় না এবং পুনরায় চেষ্টা করা হয়।

CAS এর সুবিধা

  1. লক-ফ্রি মেকানিজম: এটি কোনো লক ছাড়াই কনকারেন্ট ডেটা মডিফিকেশন পরিচালনা করে।
  2. পারফরম্যান্স বৃদ্ধি: লক-ফ্রি অপারেশন হওয়ায় এটি থ্রেড ব্লকিং সমস্যা কমায়।
  3. নির্ভুলতা: একাধিক থ্রেডের মধ্যে ডেটা মডিফিকেশন কনসিস্টেন্ট রাখে।

CAS উদাহরণ: AtomicInteger

import java.util.concurrent.atomic.AtomicInteger;

public class CASExample {
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(5); // শুরুতে ভ্যালু 5

        // বর্তমান ভ্যালু 5 এবং প্রত্যাশিত ভ্যালু 5 হলে 10 সেট করবে
        boolean isUpdated = atomicInteger.compareAndSet(5, 10);

        if (isUpdated) {
            System.out.println("Value updated successfully to: " + atomicInteger.get());
        } else {
            System.out.println("Update failed. Current value: " + atomicInteger.get());
        }

        // পুনরায় চেষ্টা করে নতুন ভ্যালু সেট করার উদাহরণ
        isUpdated = atomicInteger.compareAndSet(5, 15); // প্রত্যাশিত ভ্যালু 5 নয়
        if (!isUpdated) {
            System.out.println("Failed to update as expected value did not match. Current value: " + atomicInteger.get());
        }
    }
}

CAS ব্যবহার করে কাস্টম স্পিনলক উদাহরণ

import java.util.concurrent.atomic.AtomicBoolean;

public class CASSpinLock {
    private final AtomicBoolean lock = new AtomicBoolean(false);

    public void lock() {
        while (!lock.compareAndSet(false, true)) {
            // লক না পাওয়া পর্যন্ত অপেক্ষা
        }
    }

    public void unlock() {
        lock.set(false); // লক মুক্ত করা
    }

    public static void main(String[] args) {
        CASSpinLock spinLock = new CASSpinLock();

        Runnable task = () -> {
            spinLock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + " acquired the lock.");
                Thread.sleep(1000); // কাজ করছে
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                spinLock.unlock();
                System.out.println(Thread.currentThread().getName() + " released the lock.");
            }
        };

        Thread thread1 = new Thread(task, "Thread-1");
        Thread thread2 = new Thread(task, "Thread-2");

        thread1.start();
        thread2.start();
    }
}

CAS ভিত্তিক কনকারেন্সি কন্ট্রোল: উদাহরণ

Counter মেকানিজম

import java.util.concurrent.atomic.AtomicInteger;

class Counter {
    private final AtomicInteger count = new AtomicInteger(0);

    public void increment() {
        int current;
        int newValue;
        do {
            current = count.get(); // বর্তমান মান
            newValue = current + 1; // নতুন মান
        } while (!count.compareAndSet(current, newValue)); // পুনরাবৃত্তি যতক্ষণ না সফল হয়
    }

    public int getCount() {
        return count.get();
    }
}

public class CASCounterExample {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();

        Runnable task = () -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        };

        Thread thread1 = new Thread(task);
        Thread thread2 = new Thread(task);

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println("Final Counter Value: " + counter.getCount());
    }
}

CAS এর সীমাবদ্ধতা

  1. ABA সমস্যা: ভ্যালু পরিবর্তিত হয় এবং পুনরায় সেট হয়, যা CAS মেকানিজমের জন্য বিভ্রান্তি সৃষ্টি করতে পারে। এটি প্রতিরোধ করতে AtomicStampedReference ব্যবহার করা হয়।
  2. ব্যয়বহুল স্পিনলুপ: অনেকবার পুনরায় চেষ্টা করলে স্পিনলুপ বেশি সময় নিতে পারে।
  3. জটিলতা: বড় স্কেলের কনকারেন্সি সমস্যায় CAS কম কার্যকর হতে পারে।

CAS মেকানিজম একটি অত্যন্ত শক্তিশালী এবং কার্যকর কৌশল, যা জাভা কনকারেন্সি উন্নত করে। এটি লক-ফ্রি অপারেশন এবং পারফরম্যান্স বৃদ্ধির জন্য ব্যবহার করা হয়। তবে ABA সমস্যার মত সীমাবদ্ধতাগুলি বিবেচনা করে উন্নত মেকানিজম ব্যবহার করার প্রয়োজন হতে পারে।

Content added By
Promotion

Are you sure to start over?

Loading...