Web Development Methods: get(), set(), compareAndSet(), ইত্যাদি গাইড ও নোট

344

AtomicReference হল Java বা মাল্টি-থ্রেডিং পরিবেশে ব্যবহৃত একটি ক্লাস, যা reference object এর মান atomic (অখণ্ড) অপারেশনের মাধ্যমে আপডেট করতে দেয়। এটি মূলত CAS (Compare-And-Swap) মেকানিজমের উপর ভিত্তি করে কাজ করে। AtomicReference একাধিক থ্রেডের মধ্যে ডেটার সঠিকতা এবং সিঙ্ক্রোনাইজেশন নিশ্চিত করতে ব্যবহৃত হয়।


AtomicReference এর কাজ

  • Atomicity: সমস্ত অপারেশন (যেমন: রিড, রাইট, আপডেট) একত্রে বা indivisible উপায়ে সম্পন্ন হয়।
  • Thread-Safety: একাধিক থ্রেড একই রেফারেন্স ভ্যালুর উপর কাজ করলেও ডেটা সঠিক থাকে।
  • Lock-Free Mechanism: লক ব্যবহারের প্রয়োজন হয় না, ফলে পারফরম্যান্স বৃদ্ধি পায়।

AtomicReference এর গুরুত্বপূর্ণ Methods

১. get()

  • বর্তমান রেফারেন্স মানটি রিটার্ন করে।
  • Syntax:

    public V get()
    
  • উদাহরণ:

    AtomicReference<String> atomicRef = new AtomicReference<>("Initial Value");
    System.out.println("Current Value: " + atomicRef.get()); // আউটপুট: Initial Value
    

২. set(V newValue)

  • রেফারেন্স মানটি সরাসরি আপডেট করে।
  • Syntax:

    public void set(V newValue)
    
  • উদাহরণ:

    atomicRef.set("Updated Value");
    System.out.println("Updated Value: " + atomicRef.get()); // আউটপুট: Updated Value
    

৩. compareAndSet(V expectedValue, V newValue)

  • CAS (Compare-And-Swap) অপারেশনের মাধ্যমে রেফারেন্স মান আপডেট করে।
  • বর্তমান মান expectedValue এর সমান হলে এটি newValue দিয়ে আপডেট হয়।
  • Syntax:

    public boolean compareAndSet(V expectedValue, V newValue)
    
  • উদাহরণ:

    boolean isUpdated = atomicRef.compareAndSet("Initial Value", "New Value");
    System.out.println("Was Updated: " + isUpdated); // আউটপুট: true
    System.out.println("Current Value: " + atomicRef.get()); // আউটপুট: New Value
    

৪. getAndSet(V newValue)

  • বর্তমান রেফারেন্স মানটি রিটার্ন করে এবং নতুন মান দিয়ে আপডেট করে।
  • Syntax:

    public V getAndSet(V newValue)
    
  • উদাহরণ:

    String oldValue = atomicRef.getAndSet("Another Value");
    System.out.println("Old Value: " + oldValue); // আউটপুট: New Value
    System.out.println("Current Value: " + atomicRef.get()); // আউটপুট: Another Value
    

৫. weakCompareAndSet(V expectedValue, V newValue)

  • একটি দুর্বল বা সম্ভাব্য compare-and-set অপারেশন সম্পন্ন করে। এটি সফল হতে পারে বা ব্যর্থ হতে পারে।
  • Syntax:

    public boolean weakCompareAndSet(V expectedValue, V newValue)
    

৬. accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction)

  • নির্দিষ্ট একটি accumulator function ব্যবহার করে মানটি আপডেট করে।
  • Syntax:

    public V accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction)
    
  • উদাহরণ:

    AtomicReference<Integer> atomicInt = new AtomicReference<>(10);
    atomicInt.accumulateAndGet(5, (current, update) -> current + update); 
    System.out.println("Accumulated Value: " + atomicInt.get()); // আউটপুট: 15
    

৭. updateAndGet(UnaryOperator<V> updateFunction)

  • একটি UnaryOperator ব্যবহার করে বর্তমান মানটি আপডেট করে এবং নতুন মান রিটার্ন করে।
  • Syntax:

    public V updateAndGet(UnaryOperator<V> updateFunction)
    
  • উদাহরণ:

    atomicRef.updateAndGet(value -> value + " Updated Again");
    System.out.println("Current Value: " + atomicRef.get()); // আউটপুট: Another Value Updated Again
    

AtomicReference উদাহরণ

import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceExample {
    public static void main(String[] args) {
        AtomicReference<String> atomicRef = new AtomicReference<>("Initial Value");

        // get() Method
        System.out.println("Initial Value: " + atomicRef.get());

        // set() Method
        atomicRef.set("Updated Value");
        System.out.println("After Set: " + atomicRef.get());

        // compareAndSet() Method
        boolean wasUpdated = atomicRef.compareAndSet("Updated Value", "New Value");
        System.out.println("Was Updated: " + wasUpdated);
        System.out.println("Current Value: " + atomicRef.get());

        // getAndSet() Method
        String oldValue = atomicRef.getAndSet("Final Value");
        System.out.println("Old Value: " + oldValue);
        System.out.println("Current Value: " + atomicRef.get());
    }
}

AtomicReference এর সুবিধা

  1. Thread Safety: একাধিক থ্রেডের মধ্যে ডেটার সঠিকতা নিশ্চিত করে।
  2. Lock-Free Synchronization: লক ছাড়া ডেটা সিঙ্ক্রোনাইজ করে, যা পারফরম্যান্স উন্নত করে।
  3. CAS মেকানিজম: Compare-And-Swap এর মাধ্যমে ডেটা হস্তক্ষেপ প্রতিরোধ করে।
  4. Concurrent Programming সহজ করে।

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

  1. Complexity: বড় ডেটাসেট বা জটিল ডেটা স্ট্রাকচারের জন্য কার্যকর নয়।
  2. CPU Overhead: CAS অপারেশন বারবার ব্যর্থ হলে spinning ঘটতে পারে, যা CPU চক্র অপচয় করে।
  3. Limited Scope: শুধুমাত্র একক রেফারেন্স মান ম্যানেজ করে।

উপসংহার

AtomicReference হল মাল্টি-থ্রেডিং পরিবেশে ডেটার উপর কাজ করার একটি অত্যন্ত কার্যকর টুল। এটি thread-safe operations পরিচালনা করতে সক্ষম এবং CAS মেকানিজম ব্যবহার করে ডেটা সঠিকতা বজায় রাখে। ছোট স্কেলের synchronization problems সমাধানের জন্য এটি একটি আদর্শ সমাধান।

Content added By
Promotion

Are you sure to start over?

Loading...