java并发AtomicReference

AtomicReference的作用

已经介绍过AtomicInteger,AtomicIntegerArray,AtomicReference是针对对象的。

他通过CAS和volatile保证了对象操作的原子性和可见性,既然是引用,就会造成引用对象的变化。

    public AtomicReference(V initialValue) {
value = initialValue;
}

如果value改变了对象的属性,initialValue的属性也会改变

CAS函数的实现

    /**
* Atomically sets the value to the given updated value
* if the current value {@code ==} the expected value.
* @param expect the expected value
* @param update the new value
* @return {@code true} if successful. False return indicates that
* the actual value was not equal to the expected value.
*/
public final boolean compareAndSet(V expect, V update) {
return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
}

例子

package javalearn.javabase.thread.atomic;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j; import java.util.concurrent.atomic.AtomicReference; @Slf4j
public class AtomicReferanceTest {
public static void main(String[] args) {
Persion p1 = new Persion("jack", 1);
Persion p2 = new Persion("linda", 2);
Persion p3 = new Persion("tony", 3);
log.info("persion 3 :{}",p3.toString()); AtomicReference<Persion> atomicReference =new AtomicReference(p3);
log.info("AtomicReference is :{}",atomicReference.toString());
atomicReference.compareAndSet(p3,p1);
log.info("AtomicReference is :{}",atomicReference.toString());
log.info("persion 3 :{}",p3.toString());
atomicReference.set(p2);
log.info("AtomicReference is :{}",atomicReference.toString());
log.info("persion 3 :{}",p3.toString());
atomicReference.get().setId(33);
log.info("AtomicReference is :{}",atomicReference.toString());
log.info("AtomicReference is :{}",atomicReference.toString());
log.info("persion 3 :{}",p3.toString());
log.info("persion 2 :{}",p2.toString()); } @AllArgsConstructor
@Data
@ToString
static class Persion {
private String name;
private int id; }
}

结果


13:34:22.313 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=tony, id=3)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=jack, id=1)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=linda, id=2)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=linda, id=33)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=linda, id=33)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 2 :AtomicReferanceTest.Persion(name=linda, id=33)

java并发AtomicReference的更多相关文章

  1. Java并发AtomicReference类

    java.util.concurrent.atomic.AtomicReference类提供了可以原子读取和写入的底层对象引用的操作,还包含高级原子操作. AtomicReference支持对底层对象 ...

  2. JAVA并发编程J.U.C学习总结

    前言 学习了一段时间J.U.C,打算做个小结,个人感觉总结还是非常重要,要不然总感觉知识点零零散散的. 有错误也欢迎指正,大家共同进步: 另外,转载请注明链接,写篇文章不容易啊,http://www. ...

  3. Java并发集合的实现原理

    本文简要介绍Java并发编程方面常用的类和集合,并介绍下其实现原理. AtomicInteger 可以用原子方式更新int值.类 AtomicBoolean.AtomicInteger.AtomicL ...

  4. Java 并发工具包 java.util.concurrent 用户指南

    1. java.util.concurrent - Java 并发工具包 Java 5 添加了一个新的包到 Java 平台,java.util.concurrent 包.这个包包含有一系列能够让 Ja ...

  5. java并发:线程同步机制之Volatile关键字&原子操作Atomic

    volatile关键字 volatile是一个特殊的修饰符,只有成员变量才能使用它,与Synchronized及ReentrantLock等提供的互斥相比,Synchronized保证了Synchro ...

  6. Java并发编程实现概览

    并发概览 >>同步 如何同步多个线程对共享资源的访问是多线程编程中最基本的问题之一.当多个线程并发访问共享数据时会出现数据处于计算中间状态或者不一致的问题,从而影响到程序的正确运行.我们通 ...

  7. 《Java并发编程实战》学习笔记 线程安全、共享对象和组合对象

    Java Concurrency in Practice,一本完美的Java并发参考手册. 查看豆瓣读书 推荐:InfoQ迷你书<Java并发编程的艺术> 第一章 介绍 线程的优势:充分利 ...

  8. [转]Java并发的四种风味:Thread、Executor、ForkJoin和Actor

    这篇文章讨论了Java应用中并行处理的多种方法.从自己管理Java线程,到各种更好几的解决方法,Executor服务.ForkJoin 框架以及计算中的Actor模型. Java并发编程的4种风格:T ...

  9. Java并发编程-并发工具包(java.util.concurrent)使用指南(全)

    1. java.util.concurrent - Java 并发工具包 Java 5 添加了一个新的包到 Java 平台,java.util.concurrent 包.这个包包含有一系列能够让 Ja ...

随机推荐

  1. Centos610 Oracle 监听文件配置参考

    lister.ora配置参考 # listener.ora Network Configuration File: /home/oracle/app/oracle/product//dbhome_1/ ...

  2. Python爬虫教程:requests模拟登陆github

    1. Cookie 介绍 HTTP 协议是无状态的.因此,若不借助其他手段,远程的服务器就无法知道以前和客户端做了哪些通信.Cookie 就是「其他手段」之一. Cookie 一个典型的应用场景,就是 ...

  3. PyQt5的菜单栏、工具栏和状态栏

    1.创建菜单栏import sys, mathfrom PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore impor ...

  4. js中数组的循环与遍历forEach,map

    对于前端的循环遍历我们知道有 针对js数组的forEach().map().filter().reduce()方法 针对js对象的for/in语句(for/in也能遍历数组,但不推荐) 针对jq数组/ ...

  5. 在Eclipse或Myeclipse安装Maven插件的几种方法

    http://blog.csdn.net/lfsfxy9/article/details/9397937

  6. 最全BT磁力搜索引擎索引(整理分享,每日更新)

    btaa.xyz:http://www.veee.xyz/(可以访问,知名的BT磁力搜索,资源多,建议手机访问) 以下无法访问 idope.se:https://idope.se/(无法访问,资源丰富 ...

  7. inode节点使用率过大处理

    当发现某个分区下的inode使用率过大时,需要找到该分区下的某些目录里有哪些文件可以清理. 查找某个目录下一个月或两个月之前的文件,然后删除# find . -type f -mtime +30 |w ...

  8. SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible

    SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible ...

  9. python字典操作方法详解

    前言 字典是一种通过名字或者关键字引用的得数据结构,key 类型需要时被哈希,其键可以是数字.字符串.元组,这种结构类型也称之为映射.字典类型是Python中唯一內建的映射类型. 注意,浮点数比较很不 ...

  10. CSS背景图片设置

    *{ margin:0px; padding:0px; list-style: none; text-decoration: none; font-family: Arial,'Microsoft Y ...