public final int addandget(int delta)
atomically adds the given value to the current value, with memory effects as specified by varhandle.getandadd(java.lang.object…).
parameters:
delta - the value to add
returns:
the updated value
在java的源码中
public final int addandget(int delta) {
return u.getandaddint(this, value, delta) + delta;
}
private static final long value = u.objectfieldoffset(atomicinteger.class, "value");
继续往下执行:
/**
* atomically adds the given value to the current value of a field
* or array element within the given object {@code o}
* at the given {@code offset}.
* @param o object/array to update the field/element in
* @param offset field/element offset
* @param delta the value to add
* @return the previous value
* @since 1.8
**/
@hotspotintrinsiccandidate
public final int getandaddint(object o, long offset, int delta) {
int v;
do {
v = getintvolatile(o, offset);
} while (!weakcompareandsetint(o, offset, v, v + delta));
return v;
}
@hotspotintrinsiccandidate
public final boolean weakcompareandsetint(object o, long offset,
int expected,
int x) {
return compareandsetint(o, offset, expected, x);
}