原子更新引用

  • AtomicReference:用于对引用的原子更新
  • AtomicMarkableReference:带版本戳的原子引用类型,版本戳为boolean类型。
  • AtomicStampedReference:带版本戳的原子引用类型,版本戳为int类型。
  • package com.xdclass.atomic.demo4;
    
    import java.util.concurrent.atomic.AtomicReference;
    
    public class AtomicReferenceDemo {
    
    
        public static void main(String[] args) {
            AtomicReference<Student> atomicReference = new AtomicReference();
            Student xdclass = new Student(1L, "xdclass");
            Student wiggin = new Student(2L, "wiggin");
            atomicReference.set(xdclass);
            atomicReference.compareAndSet(xdclass, wiggin);
            Student student = atomicReference.get();
            System.out.println(student.getName());
        }
    }
    
    class Student{
        volatile long id;
        volatile String name;
    
        public Student(Long id,String name){
            this.id= id;
            this.name = name;
        }
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

猜你喜欢

转载自blog.csdn.net/u013008898/article/details/111869714