一、复习
二、完整的LongAdder类源码
package com.ruigege.AtomicOperationClass4;
import java.util.function.LongBinaryOperator;
import sun.misc.Unsafe;
@sun.misc.Contended public class LongAdderTest {
volatile long value;
public LongAdderTest(long value) {
this.value = value;
}
private static final Unsafe unsafe;
private static final long valueOffset;
static {
try {
unsafe = Unsafe.getUnsafe();
Class<?> ak = Cell.class;
valueOffset = unsafe.objectFieldOffset(ak.getDeclaredField("value"));
}catch(Exception e) {
throw new Error(e);
}
}
final long cas(long cmp,long val) {
return unsafe.compareAndSwapLong(this,valueOffset,cmp,val);
}
public long sum() {
Cell[] as = cells;
long sum = base;
if(as != null) {
for (int i=0;i<as.length;i++) {
if(Cell[i] != null) {
sum += Cell[i].value;
}
}
}
}
public void reset() {
Cell[] as = cells;
base = 0L;
if(as != null) {
for (int i=0;i<as.length;i++) {
if(as[i] != null) {
as[i].value = 0L;
}
}
}
}
public long sunThenReset() {
Cell[] as = cells;
int sum = base;
if(as != null) {
for (int i=0;i<as.length;i++) {
if(as[i] != null) {
sum += as[i];
as[i] = 0L;
}
}
}
}
public void add(long x) {
Cell[] as;
long b,v;
int m;
Cell a;
if((as = cells) != null) || !caseBase(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);
}
}
}
final boolean casBase(long cmp,long val) {
return UNSAFE.compareAndSwapLong(this,BASE,cmp,val);
}
final void longAccumulate(long x,LongBinaryOperator fn,boolean wasUncontended) {
int h;
if(h = getProbe() == 0) {
ThreadLocalRandom.current();
h = getProbe();
wasUncontended = true;
}
boolean collide = false;
for(;;) {
Cell[] as;Cell a;int n;long v;
if((as=cells) != null && (n=as.length)>0) {
if((a=as[(n-1) &h]) == null) {
if(cellsBusy == 0) {
Cell r = new Cell(x);
if(cellsBusy == 0 && casCellsBusy()) {
boolean created = false;
}
try {
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;
}
}
collide = false;
}else if(!wasUntended) {
wasUncontended = true;
}else if(a.cas(v=a.value,((fn==null)?v+x : fn.applyAsLong(v,x)))) {
break;
}else if(n >= NCPU || cells != as) {
collide = false;
}else if(!collide) {
collide = ture;
}else if(cellsBusy == 0 && casCellsBusy()) {
try {
if(cells == as) {
Cell[] rs = new Cell[n<<1];
for(int i=0;i<n;++i) {
rs[i] = as[i];
}
cells = rs;
}
}finally {
cellsBusy = 0;
}
collide = false;
continue;
}
h = advanceProbe(h);
}
else if(cellsBusy == 0 && cells == as && casCellsBusy()){
boolean init = false;
try {
if(cells == as) {
Cell[] rs = new Cell[2];
rs[h&l] = new 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;
}
}
}
三、源码:
- 所在包:com.ruigege.AtomicOperationClass4
https://github.com/ruigege66/ConcurrentJava
- 欢迎关注微信公众号:傅里叶变换,个人账号,仅用于技术交流