6.9 LongAdder
// Striped64 部分代码
package java.util.concurrent.atomic;
import java.util.function.LongBinaryOperator;
import java.util.function.DoubleBinaryOperator;
import java.util.concurrent.ThreadLocalRandom;
@SuppressWarnings("serial")
abstract class Striped64 extends Number {
transient volatile Cell[] cells;
transient volatile long base;
transient volatile int cellsBusy;
}
6.9.1 base
base - 基础值
当线程间无竞争或存在良性竞争时,对共享变量的写操作累加在 base 上
注:
无竞争:仅有 t1 线程对共享变量进行写操作
良性竞争:t1 线程和 t2 线程均对共享变量进行写操作,但非同时发生,而是有先后顺序
6.9.2 cellsBusy
cellsBusy - 锁,用于创建累加单元数组时
注:
无锁中为什么会出现锁?
累加单元数组又是什么?
别急,往后看,【6.9.3 cells】中会给你答案
CAS 锁
package com.rui.six;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.atomic.AtomicInteger;
@Slf4j(topic = "c.Test11")
public class Test11 {
public static void main(String[] args) {
log.debug("main begin...");
LockCas lc = new LockCas();
new Thread(() -> {
try {
log.debug("t1 begin...");
lc.lock();
Thread.sleep(1000);
lc.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t1").start();
new Thread(() -> {
log.debug("t2 begin...");
lc.lock();
}, "t2").start();
}
}
@Slf4j(topic = "c.LockCas")
class LockCas {
volatile AtomicInteger state = new AtomicInteger(0);
public void lock() {
while (true) {
if (state.compareAndSet(0, 1)) {
log.debug("lock...");
break;
}
}
}
public void unlock() {
log.debug("unlock...");
state.set(0);
}
}
// 某次运行结果
17:44:13 [main] c.Test11 - main begin...
17:44:13 [t1] c.Test11 - t1 begin...
17:44:13 [t1] c.LockCas - lock...
17:44:13 [t2] c.Test11 - t2 begin...
17:44:14 [t1] c.LockCas - unlock...
17:44:14 [t2] c.LockCas - lock...
进程已结束,退出代码 0
注:
上述代码仅用来协助理解 cellsBusy,实践中勿用
CAS 锁中,线程并未处于阻塞状态,而是陷入了循环中,仍处于运行状态
6.9.3 cells
cells - 累加单元数组
当线程间存在恶性竞争时,创建累加单元数组(懒惰初始化)
累加单元数组创建 1 个即可,cellsBusy 可防止创建多个
注:
【6.9.2 cellsBusy】 中的疑惑这里给出答案了哦~
Cell
package java.util.concurrent.atomic;
@SuppressWarnings("serial")
abstract class Striped64 extends Number {
@sun.misc.Contended
static final class Cell {
volatile long value;
Cell(long x) {
value = x;
}
final boolean cas(long cmp, long val) {
return UNSAFE.compareAndSwapLong(this, valueOffset, cmp, val);
}
// ...
}
}
@sun.misc.Contended - 防止缓存行伪共享
抛出两个疑问:缓存行是什么?伪共享又是什么?
要想解答这两个疑问,需先了解 CPU 缓存
当 CPU Core 发出内存访问请求时,会先查看缓存内是否有请求数据。
查看顺序:①一级指令缓存 - ②二级缓存 - ③三级缓存
如果存在(命中),则不经访问内存直接返回该数据;如果不存在(失效),则要先把内存中的相应数据载入缓存,再将其返回处理器。
缓存以缓存行为单位,一般为 64 byte
如图所示,同一份数据可能会缓存在不同 CPU Core 的缓存行中[产生数据副本]
抛出一个疑问:为何 CPU Core 不直接访问内存?
L1 - 一级指令缓存;L2 - 二级缓存;L3 - 三级缓存
可见,缓存可以提高效率
CPU 为了保持数据的一致性, 规定如果某一 CPU Core 更改了数据,那么缓存该数据的其他 CPU Core 对应的缓存行必须失效
抛出一个疑问:【简问】更改了数据后,数据失效即可,为何要使缓存行失效?
缓存以缓存行为单位
OK,我觉到,到这里 【缓存行是什么?】这个问题应该解释清楚了吧
下面来解释一下 【伪共享又是什么?】这个问题
上图!!!
① 数组在内存中是连续存储的
② 一个 Cell 为 24 byte(16 byte 的对象头和 8 byte的 value)[那么两个 Cell 为 42 byte]
③ 缓存行一般为 64 byte
so,缓存行具备存储 2 个 Cell 对象的能力
那么,问题来了:上图中,倘若左 CPU Core 中更改了 Cell[0] / Cell[1],那么右 CPU Core 中缓存 Cell[0] / Cell[1] 的缓存行必须失效,反之亦然。若这种操作多来几次,岂不是很麻烦?
首先,先插一嘴,诸如上述问题中给出的例子这种,因为多个线程同时读写同一个数据而导致缓存行失效的现象称为伪共享
然后,来解决一下上述问题
再抛出一个疑问:皇上,您还记得大明湖畔的 @sun.misc.Contended 么
如果你忘了,那我来稍微点一点
[揍.jpg] @sun.misc.Contended 这可是这一小节问题的开始啊,竟然忘了,怎么敢的啊
咳咳,好啦,俺们继续[正题]
@sun.misc.Contended 的原理是:在使用此注解的对象或字段(成员变量)前后各增加 128 byte 大小的填充(padding),从而让 CPU 将对象预读至缓存时占用不同的缓存行。
简单来讲,即通过×××手段,使缓存行仅能存储 1 个对象
so,直接防止了伪共享现象的产生
OK,到这里,我认为上面抛出的所有问题均已解决
接下来,我们一起瞅瞅源码
6.9.4 源码
【源码乱糟糟】
add
public void add(long x) {
Striped64.Cell[] as;
long b, v;
int m;
Striped64.Cell a;
if ((as = cells) != null || !casBase(b = base, b + x)) {
boolean uncontended = true;
if (as == null || (m = as.length - 1) < 0 ||
(a = as[getProbe() & m]) == null ||
!(uncontended = a.cas(v = a.value, v + x)))
longAccumulate(x, null, uncontended);
}
}
cells、cells[index] 适用于线程间存在恶性竞争的情况下
base 适用于线程间无竞争或存在良性竞争的情况下
来,跟着上图思路走,别分神
开车啦~
第一步,分析 (as = cells) != null
第二步,假设 (as = cells) != null 为 false(cells 为空),分析 !casBase(b = base, b + x)
第三步
假设 !casBase(b = base, b + x) 为 false(cas base 成功),return
这条路线为当线程间无竞争或存在良性竞争时,对共享变量的写操作累加在 base 上
假设 !casBase(b = base, b + x) 为 ture(cas base 失败),longAccumulate
longAccumulate 的源码后续会介绍
回到第一步
第四步,假设 (as = cells) != null 为 true(cells 不为空),分析 (a = as[getProbe() & m]) == null
由于 “||”,所以无需分析 !casBase(b = base, b + x)
如果 (as = cells) != null 为 true,那么 as == null 和 (m = as.length - 1) < 0 均为 false
第五步,
假设 (a = as[getProbe() & m]) == null 为 true(cells[index] 没创建),longAccumulate
假设 (a = as[getProbe() & m]) == null 为 false(cells[index] 创建了),分析 !(uncontended = a.cas(v = a.value, v + x))
第五步,
假设 !(uncontended = a.cas(v = a.value, v + x)) 为 true(cas cells 失败),longAccumulate
假设 !(uncontended = a.cas(v = a.value, v + x)) 为 false(cas cells 成功),return
这条路线为当线程间存在恶性竞争时,创建 cells 以及线程所对应的 cells[index],并把对共享变量的写操作累加在 cells[index] 上
小结:
1. base 适用于线程间无竞争或存在良性竞争的情况下
线程间无竞争或存在良性竞争,对共享变量的写操作累加在 base 上 ①
2. cells、cells[index] 适用于线程间存在恶性竞争的情况下
线程间存在恶性竞争,创建 cells 以及线程所对应的 cells[index],并把对共享变量的写操作累加在 cells[index] 上 ②
================== 一条华丽的分割线 ==================
若第一个 if 条件为假,执行 ①
若第一个 if 条件为真、第二个 if 条件为假,执行 ②
若第一、二个 if 条件为均为真,表明该方法未能执行完 ②,需调用 longAccumulate
longAccumulate
final void longAccumulate(long x, LongBinaryOperator fn,
boolean wasUncontended) {
int h;
if ((h = getProbe()) == 0) {
ThreadLocalRandom.current(); // force initialization
h = getProbe();
wasUncontended = true;
}
boolean collide = false; // True if last slot nonempty
for (; ; ) {
Striped64.Cell[] as;
Striped64.Cell a;
int n;
long v;
if ((as = cells) != null && (n = as.length) > 0) {
if ((a = as[(n - 1) & h]) == null) {
if (cellsBusy == 0) { // Try to attach new Cell
Striped64.Cell r = new Striped64.Cell(x); // Optimistically create
if (cellsBusy == 0 && casCellsBusy()) {
boolean created = false;
try { // Recheck under lock
Striped64.Cell[] rs;
int m, j;
if ((rs = cells) != null &&
(m = rs.length) > 0 &&
rs[j = (m - 1) & h] == null) {
rs[j] = r;
created = true;
}
} finally {
cellsBusy = 0;
}
if (created)
break;
continue; // Slot is now non-empty
}
}
collide = false;
} else if (!wasUncontended) // CAS already known to fail
wasUncontended = true; // Continue after rehash
else if (a.cas(v = a.value, ((fn == null) ? v + x :
fn.applyAsLong(v, x))))
break;
else if (n >= NCPU || cells != as)
collide = false; // At max size or stale
else if (!collide)
collide = true;
else if (cellsBusy == 0 && casCellsBusy()) {
try {
if (cells == as) { // Expand table unless stale
Striped64.Cell[] rs = new Striped64.Cell[n << 1];
for (int i = 0; i < n; ++i)
rs[i] = as[i];
cells = rs;
}
} finally {
cellsBusy = 0;
}
collide = false;
continue; // Retry with expanded table
}
h = advanceProbe(h);
} else if (cellsBusy == 0 && cells == as && casCellsBusy()) {
boolean init = false;
try { // Initialize table
if (cells == as) {
Striped64.Cell[] rs = new Striped64.Cell[2];
rs[h & 1] = new Striped64.Cell(x);
cells = rs;
init = true;
}
} finally {
cellsBusy = 0;
}
if (init)
break;
} else if (casBase(v = base, ((fn == null) ? v + x :
fn.applyAsLong(v, x))))
break; // Fall back on using base
}
}
longAccumulate(一)
if ((as = cells) != null && (n = as.length) > 0) {
// ...
else if (a.cas(v = a.value, ((fn == null) ? v + x :
fn.applyAsLong(v, x))))
break;
else if (n >= NCPU || cells != as)
collide = false; // At max size or stale
else if (!collide)
collide = true;
else if (cellsBusy == 0 && casCellsBusy()) {
// 扩容
continue;
}
h = advanceProbe(h);
} else if (cellsBusy == 0 && cells == as && casCellsBusy()) {
//...
} else if (casBase(v = base, ((fn == null) ? v + x :
fn.applyAsLong(v, x))))
break;
第一步,分析 (as = cells) != null
第二步,假设 (as = cells) != null 为 true(cells 不为空),分析 (n = as.length) > 0)
第三步,假设 (n = as.length) > 0) 为 true(cells[index] 创建了)[第一个 if 条件成立]
第四步,分析 a.cas(v = a.value, ((fn == null) ? v + x : fn.applyAsLong(v, x)))
第五步,
假设 a.cas(v = a.value, ((fn == null) ? v + x : fn.applyAsLong(v, x))) 为 true(cas cells 累加成功),return
假设 a.cas(v = a.value, ((fn == null) ? v + x : fn.applyAsLong(v, x))) 为 false(cas cells 累加失败),分析 n >= NCPU
第六步,
假设 n >= NCPU 为 true(超过 CPU 上限),advanceProbe + continue
advanceProbe - 改变 cells[index]
假设 n >= NCPU 为 false(超过 CPU 上限),分析 cellsBusy == 0
cells != as 为 false
1. 如果 cas cells 累加失败且超过 CPU 上限,那么修改 collide 为 false 并改变 cells[index],循环。
2. 待 cas cells 累加失败且 n < NCPU 时,!collide 为 true,再将 colide 修改为 true 并改变 cells[index]
3. 再次循环,若 cas cells 仍累加失败且 n < NCPU,则分析 cellsBusy == 0
第七步,假设 cellsBusy == 0 为 true(未加锁),分析 casCellsBusy()
第八步,
假设 casCellsBusy() 为 true(加锁成功),扩容 + continue
假设 casCellsBusy() 为 false(加锁失败),advanceProbe + continue
回到第七步
第九步,假设 cellsBusy == 0 为 false(已加锁),advanceProbe + continue
小结:
[第一个 if 条件成立] 表明 cells 和 cells[index] 均已被创建
整个流程仅差 cas cells 累加成功即可圆满
所以在第一个 if 代码块中执行的操作为 cas cells
通过 advanceProbe 或者扩容的方式,不断尝试 cas cells
回到第三步
第十步,假设 (n = as.length) > 0) 为 true(cells[index] 未创建)[第一个 if 条件不成立]
回到第二步
第十一步,假设 (as = cells) != null 为 false(cells 为空),[第一个 if 条件不成立]
longAccumulate(二)
for (; ; ) {
if ((as = cells) != null && (n = as.length) > 0)
else if (cellsBusy == 0 && cells == as && casCellsBusy()) {
// 创建 cells 和 cells[index]
break;
} else if (casBase(v = base, ((fn == null) ? v + x :
fn.applyAsLong(v, x))))
break;
}
[第一个 if 条件不成立] 有两种情况:
1. cells 不为空、cells[index] 未创建
2. cells 为空
第一步,分析 cellsBusy == 0
第二步,假设 cellsBusy == 0 为 true(未加锁),分析 casCellsBusy()
cells == as 为 true
第三步,
假设 casCellsBusy() 为 true(加锁成功)[第二个 if 条件成立]
创建 cells 和 cells[index],return
小结:
[第一个 if 条件不成立] 有两种情况:
1. cells 不为空、cells[index] 未创建
2. cells 为空
第二个 if 代码块中执行的操作为 创建 cells 和 cells[index]
cells 创建 1 个即可,需配合 cellsBusy 防止创建多个
抛出一个疑问:当 cells 不为空时,已存在 cells,若再创建 cells,不就有两个 cells了么?
新的覆盖旧的(舍弃旧的)
假设 casCellsBusy() 为 false(加锁失败)[第二个 if 条件不成立]
回到第二步
第四步,假设 cellsBusy == 0 为 false(已加锁)[第二个 if 条件不成立]
longAccumulate(三)
for (; ; ) {
if ((as = cells) != null && (n = as.length) > 0)
else if (cellsBusy == 0 && cells == as && casCellsBusy())
else if (casBase(v = base, ((fn == null) ? v + x : fn.applyAsLong(v, x))))
break;
}
[第一个 if 条件不成立]&&[第二个 if 条件不成立] 有四种情况:
1. cells 不为空、cells[index] 未创建、未加锁、加锁失败
2. cells 不为空、cells[index] 未创建、已加锁
3. cells 为空、未加锁、加锁失败
4. cells 为空、已加锁
第一步,分析 casBase(v = base, ((fn == null) ? v + x : fn.applyAsLong(v, x)))
第二步,
假设 casBase(v = base, ((fn == null) ? v + x : fn.applyAsLong(v, x))) 为 true(cas base 成功),return
假设 casBase(v = base, ((fn == null) ? v + x : fn.applyAsLong(v, x))) 为 false(cas base 失败),continue
小结:
若第一个 if 条件为真,表明整个流程仅差 cas cells 累加成功即可圆满
若第一个 if 条件为假、第二个 if 条件为真,表明线程间存在恶性竞争,创建 cells 以及线程所对应的 cells[index],并把对共享变量的写操作累加在 cells[index] 上
若第一、二个 if 条件为均为假,第三个 if 条件为真,表明线程间无竞争或存在良性竞争,对共享变量的写操作累加在 base 上
若第一、二、三个 if 条件为均为假,循环直至某个 if 条件为真执行其对应操作
sum
将不为空的 cells[index] 进行累加
package java.util.concurrent.atomic;
import java.io.Serializable;
public class LongAdder extends Striped64 implements Serializable {
public long sum() {
Cell[] as = cells; Cell a;
long sum = base;
if (as != null) {
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null)
sum += a.value;
}
}
return sum;
}
}
6.10 Unsafe
CAS、LockSupport
6.10.1 概述
Unsafe 对象不能直接调用,只能通过反射获得
package com.rui.six;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class Test12 {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafe.get(null);
System.out.println(unsafe);
}
}
// 运行结果
sun.misc.Unsafe@4b67cf4d
进程已结束,退出代码 0
6.10.2 Unsafe CAS 操作
package com.rui.six;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class Test12 {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafe.get(null);
System.out.println(unsafe);
Field id = Teacher.class.getDeclaredField("id");
Field name = Teacher.class.getDeclaredField("name");
// 获得成员变量的偏移量
long idOffset = unsafe.objectFieldOffset(id);
long nameOffset = unsafe.objectFieldOffset(name);
Teacher t = new Teacher();
unsafe.compareAndSwapInt(t, idOffset, 0, 1);
unsafe.compareAndSwapObject(t, nameOffset, null, "张三");
System.out.println(t);
}
}
class Teacher {
private int id;
private String name;
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
// 运行结果
sun.misc.Unsafe@4b67cf4d
Teacher{id=1, name='张三'}
进程已结束,退出代码 0
6.10.3 Unsafe 原子整数 操作
使用自定义的 AtomicData 实现之前线程安全的原子整数 Account
package com.rui.six;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class Test13 {
public static void main(String[] args) {
Account.demo(new AccountCas(10000));
}
}
class AtomicData implements Account13 {
private volatile int balance;
static private Unsafe unsafe;
static private long balanceOffset;
public void atomicData() throws NoSuchFieldException, IllegalAccessException {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
unsafe = (Unsafe) theUnsafe.get(null);
Field balance = AtomicData.class.getDeclaredField("balance");
long balanceOffset = unsafe.objectFieldOffset(balance);
}
public AtomicData(int balance) {
this.balance = balance;
}
@Override
public Integer getBalance() {
return balance;
}
@Override
public void withdraw(Integer amount) {
while (true) {
int prev = balance;
int next = prev - amount;
if (unsafe.compareAndSwapInt(this, balanceOffset, prev, next)) ;
}
}
}
interface Account13 {
// 获取余额
Integer getBalance();
// 取款
void withdraw(Integer amount);
/**
* 方法内会启动 1000 个线程,每个线程做取款 10 元的操作
* 如果初始余额为 10000 元,那么终止余额应当是 0 元
*/
static void demo(Account account) {
List<Thread> ts = new ArrayList<>();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
ts.add(new Thread(() -> {
account.withdraw(10);
}));
}
ts.forEach(Thread::start);
ts.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
long end = System.nanoTime();
System.out.println("终止余额 = " + account.getBalance()
+ ";cost " + (end - start) / 1000_000 + " ms");
}
}
// 运行结果
终止余额 = 0;cost 98 ms
进程已结束,退出代码 0
说些废话
本篇文章为博主日常学习记录,故而会概率性地存在各种错误,若您在浏览过程中发现一些,请在评论区指正,望我们共同进步,谢谢!