AtomicReferenceFieldUpdater源码解析

AtomicReferenceFieldUpdater源码解析

AtomicReferenceFieldUpdater类介绍

AtomicReferenceFieldUpdater是Doug Lea在Java 5中写的atomic classes 中Filed Updater的一部分,本质上是volatile字段的包装器。

AtomicReferenceFieldUpdater是基于反射的工具类,用来将指定类型的指定的volatile引用字段进行原子更新,对应的原子引用字段不能是private的。通常一个类volatile成员属性获取值、设定为某个值两个操作时非原子的,若想将其变为原子的,则可通过AtomicReferenceFieldUpdater来实现。

类图

AtomicReferenceFiledUpdater类图

主要属性

AtomicReferenceFieldUpdater本身是没有属性的,但是我们可以通过它的继承类来一窥究竟。

1
2
3
4
5
6
7
8
9
10
11
private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
private final long offset;
/**
* if field is protected, the subclass constructing updater, else
* the same as tclass
*/
private final Class<?> cclass;
/** class holding the field */
private final Class<T> tclass;
/** field value type */
private final Class<V> vclass;

U:JDK提供的底层实现CAS算法的类

cclass:待更新的的对象的类的Class对象

tclass:待操作的目标对象

vclass:待操作的字段的类型

主要方法

newUpdater(Class, Class vclass, String)

创建一个新的原子更新器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* Creates and returns an updater for objects with the given field.
* The Class arguments are needed to check that reflective types and
* generic types match.
*
* @param tclass the class of the objects holding the field
* @param vclass the class of the field
* @param fieldName the name of the field to be updated
* @param <U> the type of instances of tclass
* @param <W> the type of instances of vclass
* @return the updater
* @throws ClassCastException if the field is of the wrong type
* @throws IllegalArgumentException if the field is not volatile
* @throws RuntimeException with a nested reflection-based
* exception if the class does not hold field or is the wrong type,
* or the field is inaccessible to the caller according to Java language
* access control
*/
@CallerSensitive
public static <U,W> AtomicReferenceFieldUpdater<U,W> newUpdater(Class<U> tclass,
Class<W> vclass,
String fieldName) {
return new AtomicReferenceFieldUpdaterImpl<U,W>
(tclass, vclass, fieldName, Reflection.getCallerClass());
}

AtomicReferenceFieldUpdater()

1
2
3
4
5
/**
* Protected do-nothing constructor for use by subclasses.
*/
protected AtomicReferenceFieldUpdater() {
}

compareAndSet(T,V,V)

抽象方法,具体实现看实现类

weakCompareAndSet(T,V,V)

抽象方法,具体实现看实现类

set(T,V)

抽象方法,具体实现看实现类

lazySet(T,V)

抽象方法,具体实现看实现类

get(T):V

抽象方法,具体实现看实现类

getAndSet(T,V)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* Atomically sets the field of the given object managed by this updater
* to the given value and returns the old value.
*
* @param obj An object whose field to get and set
* @param newValue the new value
* @return the previous value
*/
public V getAndSet(T obj, V newValue) {
V prev;
do {
prev = get(obj);
} while (!compareAndSet(obj, prev, newValue));
return prev;
}

返回当前的值,并将当前值设置为新值newValue。需要注意的是,此处使用的是一个死循环,在极端的多线程环境下,这个循环可能要运行很久。

getAndUpdate(T,UnaryOperator)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* Atomically updates the field of the given object managed by this updater
* with the results of applying the given function, returning the previous
* value. The function should be side-effect-free, since it may be
* re-applied when attempted updates fail due to contention among threads.
*
* @param obj An object whose field to get and set
* @param updateFunction a side-effect-free function
* @return the previous value
* @since 1.8
*/
public final V getAndUpdate(T obj, UnaryOperator<V> updateFunction) {
V prev, next;
do {
prev = get(obj);
next = updateFunction.apply(prev);
} while (!compareAndSet(obj, prev, next));
return prev;
}

获取当前值,返回。然后将当前值应用于单元函数,并将结果更新到当前值上。需要注意的是,此处使用的是一个死循环,在极端的多线程环境下,这个循环可能要运行很久。

updateAndGet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* Atomically updates the field of the given object managed by this updater
* with the results of applying the given function, returning the updated
* value. The function should be side-effect-free, since it may be
* re-applied when attempted updates fail due to contention among threads.
*
* @param obj An object whose field to get and set
* @param updateFunction a side-effect-free function
* @return the updated value
* @since 1.8
*/
public final V updateAndGet(T obj, UnaryOperator<V> updateFunction) {
V prev, next;
do {
prev = get(obj);
next = updateFunction.apply(prev);
} while (!compareAndSet(obj, prev, next));
return next;
}

将当前值应用于单元函数,并将获得的结果更新至当前值,返回。需要注意的是,此处使用的是一个死循环,在极端的多线程环境下,这个循环可能要运行很久。

getAndAccumulate(T,V,BinaryOperator)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Atomically updates the field of the given object managed by this
* updater with the results of applying the given function to the
* current and given values, returning the previous value. The
* function should be side-effect-free, since it may be re-applied
* when attempted updates fail due to contention among threads. The
* function is applied with the current value as its first argument,
* and the given update as the second argument.
*
* @param obj An object whose field to get and set
* @param x the update value
* @param accumulatorFunction a side-effect-free function of two arguments
* @return the previous value
* @since 1.8
*/
public final V getAndAccumulate(T obj, V x,
BinaryOperator<V> accumulatorFunction) {
V prev, next;
do {
prev = get(obj);
next = accumulatorFunction.apply(prev, x);
} while (!compareAndSet(obj, prev, next));
return prev;
}

获取当前值,返回。并将当前值和x应用于双元函数,将获得的结果更新当前值。需要注意的是,此处使用的是一个死循环,在极端的多线程环境下,这个循环可能要运行很久。

accumulateAndGet(T,V,BinaryOperator)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Atomically updates the field of the given object managed by this
* updater with the results of applying the given function to the
* current and given values, returning the updated value. The
* function should be side-effect-free, since it may be re-applied
* when attempted updates fail due to contention among threads. The
* function is applied with the current value as its first argument,
* and the given update as the second argument.
*
* @param obj An object whose field to get and set
* @param x the update value
* @param accumulatorFunction a side-effect-free function of two arguments
* @return the updated value
* @since 1.8
*/
public final V accumulateAndGet(T obj, V x,
BinaryOperator<V> accumulatorFunction) {
V prev, next;
do {
prev = get(obj);
next = accumulatorFunction.apply(prev, x);
} while (!compareAndSet(obj, prev, next));
return next;
}

将当前值和x应用于双元函数,并将结果更新值当前值,返回。需要注意的是,此处使用的是一个死循环,在极端的多线程环境下,这个循环可能要运行很久。

AtomicReferenceFieldUpdaterImpl(Class, Class, String, Class)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Internal type checks within all update methods contain
* internal inlined optimizations checking for the common
* cases where the class is final (in which case a simple
* getClass comparison suffices) or is of type Object (in
* which case no check is needed because all objects are
* instances of Object). The Object case is handled simply by
* setting vclass to null in constructor. The targetCheck and
* updateCheck methods are invoked when these faster
* screenings fail.
*/

AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
final Class<V> vclass,
final String fieldName,
final Class<?> caller) {
final Field field;
final Class<?> fieldClass;
final int modifiers;
try {
// 当前字段是否存在
field = AccessController.doPrivileged(
new PrivilegedExceptionAction<Field>() {
public Field run() throws NoSuchFieldException {
return tclass.getDeclaredField(fieldName);
}
});
// 确认字段有访问权限
modifiers = field.getModifiers();
sun.reflect.misc.ReflectUtil.ensureMemberAccess(
caller, tclass, null, modifiers);
ClassLoader cl = tclass.getClassLoader();
ClassLoader ccl = caller.getClassLoader();
// 判断class和修改的字段的类加载器是否在一个类加载器委托链中
if ((ccl != null) && (ccl != cl) &&
((cl == null) || !isAncestor(cl, ccl))) {
sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
}
fieldClass = field.getType();
} catch (PrivilegedActionException pae) {
throw new RuntimeException(pae.getException());
} catch (Exception ex) {
throw new RuntimeException(ex);
}

if (vclass != fieldClass)
throw new ClassCastException();
// 必须是引用类型
if (vclass.isPrimitive())
throw new IllegalArgumentException("Must be reference type");

// 必须被字段 volatile修饰
if (!Modifier.isVolatile(modifiers))
throw new IllegalArgumentException("Must be volatile type");

// Access to protected field members is restricted to receivers only
// of the accessing class, or one of its subclasses, and the
// accessing class must in turn be a subclass (or package sibling)
// of the protected member's defining class.
// If the updater refers to a protected field of a declaring class
// outside the current package, the receiver argument will be
// narrowed to the type of the accessing class.
this.cclass = (Modifier.isProtected(modifiers) &&
tclass.isAssignableFrom(caller) &&
!isSamePackage(tclass, caller))
? caller : tclass;
this.tclass = tclass;
this.vclass = vclass;
this.offset = U.objectFieldOffset(field);
}

生成AtomicReferenceFieldUpdater对象。

原子更新器的使用存在比较苛刻的条件如下

  • 操作的字段不能是static类型。
  • 操作的字段不能是final类型的,因为final根本没法修改。
  • 字段必须是volatile修饰的,也就是数据本身是读一致的。
  • 属性必须对当前的Updater所在的区域是可见的,如果不是当前类内部进行原子更新器操作不能使用private,protected子类操作父类时修饰符必须是protect权限及以上,如果在同一个package下则必须是default权限及以上,也就是说无论何时都应该保证操作类与被操作类间的可见性。

isAncestor(ClassLoader, ClassLoader)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* Returns true if the second classloader can be found in the first
* classloader's delegation chain.
* Equivalent to the inaccessible: first.isAncestor(second).
*/
private static boolean isAncestor(ClassLoader first, ClassLoader second) {
ClassLoader acl = first;
do {
acl = acl.getParent();
if (second == acl) {
return true;
}
} while (acl != null);
return false;
}

判断两个classes是否是相同的类加载器和包修饰

isSamePackage(Class, Class)

1
2
3
4
5
6
7
8
/**
* Returns true if the two classes have the same class loader and
* package qualifier
*/
private static boolean isSamePackage(Class<?> class1, Class<?> class2) {
return class1.getClassLoader() == class2.getClassLoader()
&& Objects.equals(getPackageName(class1), getPackageName(class2));
}

判断两个类是否具有相同的包名

getPackageName(Class)

1
2
3
4
5
private static String getPackageName(Class<?> cls) {
String cn = cls.getName();
int dot = cn.lastIndexOf('.');
return (dot != -1) ? cn.substring(0, dot) : "";
}

获取类的包名称

accessCheck(T)

1
2
3
4
5
6
7
8
/**
* Checks that target argument is instance of cclass. On
* failure, throws cause.
*/
private final void accessCheck(T obj) {
if (!cclass.isInstance(obj))
throwAccessCheckException(obj);
}

确认目标对象obj是cclass的实例。

throwAccessCheckException(T)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* Throws access exception if accessCheck failed due to
* protected access, else ClassCastException.
*/
private final void throwAccessCheckException(T obj) {
if (cclass == tclass)
throw new ClassCastException();
else
throw new RuntimeException(
new IllegalAccessException(
"Class " +
cclass.getName() +
" can not access a protected member of class " +
tclass.getName() +
" using an instance of " +
obj.getClass().getName()));
}

如果待持有的字段受保护或者类型转换失败,抛出异常

valueCheck(V)

1
2
3
4
private final void valueCheck(V v) {
if (v != null && !(vclass.isInstance(v)))
throwCCE();
}

校验v是否是vclass的实例

throwCCE()

1
2
3
static void throwCCE() {
throw new ClassCastException();
}

抛出ClassCaseException异常

compareAndSet(T,V,V)

1
2
3
4
5
public final boolean compareAndSet(T obj, V expect, V update) {
accessCheck(obj);
valueCheck(update);
return U.compareAndSwapObject(obj, offset, expect, update);
}

先校验obj是否是T的实例,再校验update是否是V 的实例。

然后再当前值等同于except时,将当前值更新为update,并返回true,否则返回false。

weakCompareAndSet(T,V,V)

1
2
3
4
5
6
public final boolean weakCompareAndSet(T obj, V expect, V update) {
// same implementation as strong form for now
accessCheck(obj);
valueCheck(update);
return U.compareAndSwapObject(obj, offset, expect, update);
}

实现同compareAndSet

set(T,V)

1
2
3
4
5
public final void set(T obj, V newValue) {
accessCheck(obj);
valueCheck(newValue);
U.putObjectVolatile(obj, offset, newValue);
}

先校验obj是否是T的实例,再校验update是否是V 的实例。最后再设置当前值为newValue。

lazySet(T,V)

1
2
3
4
5
public final void lazySet(T obj, V newValue) {
accessCheck(obj);
valueCheck(newValue);
U.putOrderedObject(obj, offset, newValue);
}

先校验obj是否是T的实例,再校验update是否是V 的实例。最后在“慢慢”把当前值设置为newValue。这里“慢慢”表示,在多线程环境下,会有一段时间,其他线程还是能读取到旧值的。

get(T)

1
2
3
4
5
@SuppressWarnings("unchecked")
public final V get(T obj) {
accessCheck(obj);
return (V)U.getObjectVolatile(obj, offset);
}

先校验obj是否是T的实例,再返回对应的值。

getAndSet(T,V)

1
2
3
4
5
6
@SuppressWarnings("unchecked")
public final V getAndSet(T obj, V newValue) {
accessCheck(obj);
valueCheck(newValue);
return (V)U.getAndSetObject(obj, offset, newValue);
}

先校验obj是否是T的实例,再校验update是否是V 的实例。最后返回当前值,并把当前值设置为newValue。

应用

看下面这个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
public class AtomicReferTest {
public static void main(String[] args) throws Exception {
AtomicReferenceFieldUpdater updater = AtomicReferenceFieldUpdater.newUpdater(Dog.class, String.class,"name");
Dog dog1 = new Dog();
System.out.println(updater.compareAndSet(dog1, "dog1", "compareAndSet"));
System.out.println(dog1.name);
System.out.println(updater.getAndSet(dog1, "getAndSet"));
System.out.println(dog1.name);
}
}
class Dog {
volatile String name = "dog1";
}

输出结果

1
2
3
4
true
compareAndSet
compareAndSet
getAndSet

参考资料

Java原子属性更新器AtomicReferenceFieldUpdater的使用