16 Ekim 2023 Pazartesi

AtomicMarkableReference Sınıfı

Giriş
Şu satırı dahil ederiz.
import java.util.concurrent.atomic.AtomicMarkableReference;
Açıklaması şöyle.
Provides atomic operations for markable reference variables. The primary difference between AtomicReference and AtomicMarkableReference is the presence of the “mark” boolean in the latter. Internally it saves a Pair.of(value, mark).
Örnek
Şöyle yaparız
AtomicMarkableReference<String> ref = new AtomicMarkableReference<>("Initial", false);

System.out.println("Current Reference: " + ref.getReference());
System.out.println("Current Mark: " + ref.isMarked());

// Set a new reference with a mark
ref.set("Updated", true);
System.out.println("Updated Reference: " + ref.getReference());
System.out.println("Updated Mark: " + ref.isMarked());

// Compare and set reference and mark
boolean success = ref.compareAndSet("Updated", "NewValue", true, false);
System.out.println("Compare and Set Success: " + success);
System.out.println("Updated Reference: " + ref.getReference());
System.out.println("Updated Mark: " + ref.isMarked());


Hiç yorum yok:

Yorum Gönder