atomicReference 可以保证对象的原子操作.

 public static void main(String[] args) {


        AtomicReference<Simple> atomic = new AtomicReference<>(new Simple("xiaodao",23));


        System.out.println(atomic.get());
        boolean result = atomic.compareAndSet(atomic.get(), new Simple("bbb", 90));

        System.out.println(result);




    }

    static class Simple{
        private String name;
        private int  age;

        public Simple(String name, int age) {
            this.name = name;
            this.age = age;
        }


        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
View Code

相关文章:

  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-13
  • 2022-12-23
  • 2022-12-23
  • 2021-10-16
  • 2021-08-18
相关资源
相似解决方案