AtomicReference
public class AtomicReference<V> implements java.io.Serializable {
private static final long serialVersionUID = -1848883965231344442L;
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long valueOffset;
static {
try {
valueOffset = unsafe.objectFieldOffset(AtomicReference.class.getDeclaredField("value"));
} catch (Exception ex) { throw new Error(ex); }
}
private volatile V value;
public AtomicReference(V initialValue) {
value = initialValue;
}
public AtomicReference() {
}
public final V get() {
return value;
}
public final void set(V newValue) {
value = newValue;
}
public final void lazySet(V newValue) {
unsafe.putOrderedObject(this, valueOffset, newValue);
}
public final boolean compareAndSet(V expect, V update) {
return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
}
public final boolean weakCompareAndSet(V expect, V update) {
return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
}
@SuppressWarnings("unchecked")
public final V getAndSet(V newValue) {
return (V)unsafe.getAndSetObject(this, valueOffset, newValue);
}
public final V getAndUpdate(UnaryOperator<V> updateFunction) {
V prev, next;
do {
prev = get();
next = updateFunction.apply(prev);
} while (!compareAndSet(prev, next));
return prev;
}
public final V updateAndGet(UnaryOperator<V> updateFunction) {
V prev, next;
do {
prev = get();
next = updateFunction.apply(prev);
} while (!compareAndSet(prev, next));
return next;
}
public final V getAndAccumulate(V x,BinaryOperator<V> accumulatorFunction) {
V prev, next;
do {
prev = get();
next = accumulatorFunction.apply(prev, x);
} while (!compareAndSet(prev, next));
return prev;
}
public final V accumulateAndGet(V x,BinaryOperator<V> accumulatorFunction) {
V prev, next;
do {
prev = get();
next = accumulatorFunction.apply(prev, x);
} while (!compareAndSet(prev, next));
return next;
}
public String toString() {
return String.valueOf(get());
}
}
AtomicReference的更多相关文章
- java并发J.U.C AtomicReference VS Volatile
SpinLock实现,摘自并发编程网 package com.juc.simple; import java.util.concurrent.atomic.AtomicReference; /** * ...
- 【实战Java高并发程序设计 2】无锁的对象引用:AtomicReference
AtomicReference和AtomicInteger非常类似,不同之处就在于AtomicInteger是对整数的封装,而AtomicReference则对应普通的对象引用.也就是它可以保证你在修 ...
- java多线程系类:JUC原子类:04之AtomicReference原子类
概要 本章对AtomicReference引用类型的原子类进行介绍.内容包括:AtomicReference介绍和函数列表AtomicReference源码分析(基于JDK1.7.0_40)Atomi ...
- Java多线程系列--“JUC原子类”04之 AtomicReference原子类
概要 本章对AtomicReference引用类型的原子类进行介绍.内容包括:AtomicReference介绍和函数列表AtomicReference源码分析(基于JDK1.7.0_40)Atomi ...
- 可重入锁 & 自旋锁 & Java里的AtomicReference和CAS操作 & Linux mutex不可重入
之前还是写过蛮多的关于锁的文章的: http://www.cnblogs.com/charlesblc/p/5994162.html <[转载]Java中的锁机制 synchronized &a ...
- 【转】AtomicReference与volatile的区别
来源:AtomicReference与volatile的区别 AtomicReference与volatile的在可见性上的意义是一致的. volatile不能保证原子性,AutomicReferen ...
- java多线程--AtomicReference
AtomicReference介绍 AtomicReference是作用是对"对象"进行原子操作. AtomicReference源码分析(基于JDK1.7.0_40) 在JDK1 ...
- AtomicReference实现单例模式
CAS是项乐观锁技术,当多个线程尝试使用CAS同时更新同一个变量时,只有其中一个线程能更新变量的值,而其它线程都失败,失败的线程并不会被挂起,而是被告知这次竞争中失败,并可以再次尝试. 乐观锁的一种实 ...
- AtomicReference,AtomicStampedReference与AtomicMarkableReference的区别
AtomicReference 通过volatile和Unsafe提供的CAS函数实现原子操作. 自旋+CAS的无锁操作保证共享变量的线程安全 value是volatile类型,这保证了:当某线程修改 ...
随机推荐
- 剑指offer——python【第4题】重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- linux安装杀毒软件
https://www.cnblogs.com/bingo1024/p/9018212.html
- radio选中与不选中
$("input[name='bankCardType']").val("sdf"); $('input:radio[name="bankCardTy ...
- Python文件的读写
一.写数据 f = open("hello.txt", "w") f.write("hello world python!") f.clos ...
- mysql--表数据操作
查询: 简单查询 ####查询的字段必须在表中存在 #### 对查询出来的数据进行修改时,不会修改原来的数据,只是修改了可视化的,我们看到的数据. # 查一个数据 select 字段名 from 表名 ...
- 2019-oo-第一单元总结
第一单元总结 ——表达式的求导 一.思路综述 二.代码分析 结构分析 bug分析 风格分析 三.Hack Hack Hack 四.难点总结 五.感想 一.思路综述 第一次作业 输入处理时,一项一项地用 ...
- 如何彻底禁止win10易升更新(转)
原文:https://blog.csdn.net/qq_33075489/article/details/79755896 add by zhj: 第二步是我自己加的 Win10版本:家庭中文版64位 ...
- 使用Eclipse来操作HDFS的文件
一.常用类 1.Configuration Hadoop配置文件的管理类,该类的对象封装了客户端或者服务器的配置(配置集群时,所有的xml文件根节点都是configuration) 创建一个Confi ...
- python一次性解压多层嵌套zip压缩包
zip包里的结构不是固定的,有可能只需要解压一次就完成了,有可能解压后里面还存在zip文件,需要继续进行解压缩 写了个简单的递归函数来实现解压非固定结构zip包,若解压后的zip子目录下仍含有zip文 ...
- centos禁ping
Linux默认是允许Ping响应的,系统是否允许Ping由2个因素决定的:A.内核参数,B.防火墙,需要2个因素同时允许才能允许Ping,2个因素有任意一个禁Ping就无法Ping. 具体的配置方法如 ...