MCPcopy Create free account
hub / github.com/ReadyTalk/avian / AtomicReference

Class AtomicReference

classpath/java/util/concurrent/atomic/AtomicReference.java:17–75  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15import sun.misc.Unsafe;
16
17public class AtomicReference<T> implements java.io.Serializable {
18 private static final long serialVersionUID = -1848883965231344442L;
19
20 private static final Unsafe unsafe = Unsafe.getUnsafe();
21 private static final long valueOffset;
22
23 static {
24 try {
25 Field<?> f = AtomicReference.class.getDeclaredField("value");
26 valueOffset = unsafe.objectFieldOffset(f);
27 } catch (NoSuchFieldException e) {
28 throw new Error(e);
29 }
30 }
31
32 private volatile T value;
33
34 public AtomicReference() {
35 this(null);
36 }
37
38 public AtomicReference(T initialValue) {
39 this.value = initialValue;
40 }
41
42 public T get() {
43 return value;
44 }
45
46 public void set(T newValue) {
47 value = newValue;
48 }
49
50 public void lazySet(T newValue) {
51 unsafe.putOrderedObject(this, valueOffset, newValue);
52 }
53
54 public boolean compareAndSet(T expect, T update) {
55 return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
56 }
57
58 public boolean weakCompareAndSet(T expect, T update) {
59 return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
60 }
61
62 public final T getAndSet(T newValue) {
63 while (true) {
64 T current = value;
65 if (compareAndSet(current, newValue)) {
66 return current;
67 }
68 }
69 }
70
71 @Override
72 public String toString() {
73 return String.valueOf(value);
74 }

Callers

nothing calls this directly

Calls 3

getUnsafeMethod · 0.95
getDeclaredFieldMethod · 0.80
objectFieldOffsetMethod · 0.80

Tested by

no test coverage detected