KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > concurrent > atomic > AtomicReference


1 /*
2  * @(#)AtomicReference.java 1.5 04/01/12
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.concurrent.atomic;
9 import sun.misc.Unsafe;
10
11 /**
12  * An object reference that may be updated atomically. See the {@link
13  * java.util.concurrent.atomic} package specification for description
14  * of the properties of atomic variables.
15  * @since 1.5
16  * @author Doug Lea
17  * @param <V> The type of object referred to by this reference
18  */

19 public class AtomicReference<V> implements java.io.Serializable JavaDoc {
20     private static final long serialVersionUID = -1848883965231344442L;
21
22     private static final Unsafe unsafe = Unsafe.getUnsafe();
23     private static final long valueOffset;
24
25     static {
26       try {
27         valueOffset = unsafe.objectFieldOffset
28             (AtomicReference JavaDoc.class.getDeclaredField("value"));
29       } catch(Exception JavaDoc ex) { throw new Error JavaDoc(ex); }
30     }
31
32     private volatile V value;
33
34     /**
35      * Create a new AtomicReference with the given initial value.
36      *
37      * @param initialValue the initial value
38      */

39     public AtomicReference(V initialValue) {
40         value = initialValue;
41     }
42
43     /**
44      * Create a new AtomicReference with null initial value.
45      */

46     public AtomicReference() {
47     }
48   
49     /**
50      * Get the current value.
51      *
52      * @return the current value
53      */

54     public final V get() {
55         return value;
56     }
57   
58     /**
59      * Set to the given value.
60      *
61      * @param newValue the new value
62      */

63     public final void set(V newValue) {
64         value = newValue;
65     }
66   
67     /**
68      * Atomically set the value to the given updated value
69      * if the current value <tt>==</tt> the expected value.
70      * @param expect the expected value
71      * @param update the new value
72      * @return true if successful. False return indicates that
73      * the actual value was not equal to the expected value.
74      */

75     public final boolean compareAndSet(V expect, V update) {
76       return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
77     }
78
79     /**
80      * Atomically set the value to the given updated value
81      * if the current value <tt>==</tt> the expected value.
82      * May fail spuriously.
83      * @param expect the expected value
84      * @param update the new value
85      * @return true if successful.
86      */

87     public final boolean weakCompareAndSet(V expect, V update) {
88       return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
89     }
90
91     /**
92      * Set to the given value and return the old value.
93      *
94      * @param newValue the new value
95      * @return the previous value
96      */

97     public final V getAndSet(V newValue) {
98         while (true) {
99             V x = get();
100             if (compareAndSet(x, newValue))
101                 return x;
102         }
103     }
104
105     /**
106      * Returns the String representation of the current value.
107      * @return the String representation of the current value.
108      */

109     public String JavaDoc toString() {
110         return String.valueOf(get());
111     }
112
113 }
114
Popular Tags