KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)AtomicStampedReference.java 1.4 03/12/19
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
10 /**
11  * An <tt>AtomicStampedReference</tt> maintains an object reference
12  * along with an integer "stamp", that can be updated atomically.
13  *
14  * <p> Implementation note. This implementation maintains stamped
15  * references by creating internal objects representing "boxed"
16  * [reference, integer] pairs.
17  *
18  * @since 1.5
19  * @author Doug Lea
20  * @param <V> The type of object referred to by this reference
21  */

22 public class AtomicStampedReference<V> {
23
24     private static class ReferenceIntegerPair<T> {
25         private final T reference;
26         private final int integer;
27         ReferenceIntegerPair(T r, int i) {
28             reference = r; integer = i;
29         }
30     }
31
32     private final AtomicReference JavaDoc<ReferenceIntegerPair<V>> atomicRef;
33
34     /**
35      * Creates a new <tt>AtomicStampedReference</tt> with the given
36      * initial values.
37      *
38      * @param initialRef the initial reference
39      * @param initialStamp the initial stamp
40      */

41     public AtomicStampedReference(V initialRef, int initialStamp) {
42         atomicRef = new AtomicReference JavaDoc<ReferenceIntegerPair<V>>
43             (new ReferenceIntegerPair<V>(initialRef, initialStamp));
44     }
45
46     /**
47      * Returns the current value of the reference.
48      *
49      * @return the current value of the reference
50      */

51     public V getReference() {
52         return atomicRef.get().reference;
53     }
54
55     /**
56      * Returns the current value of the stamp.
57      *
58      * @return the current value of the stamp
59      */

60     public int getStamp() {
61         return atomicRef.get().integer;
62     }
63
64     /**
65      * Returns the current values of both the reference and the stamp.
66      * Typical usage is <tt>int[1] holder; ref = v.get(holder); </tt>.
67      *
68      * @param stampHolder an array of size of at least one. On return,
69      * <tt>stampholder[0]</tt> will hold the value of the stamp.
70      * @return the current value of the reference
71      */

72     public V get(int[] stampHolder) {
73         ReferenceIntegerPair<V> p = atomicRef.get();
74         stampHolder[0] = p.integer;
75         return p.reference;
76     }
77
78     /**
79      * Atomically sets the value of both the reference and stamp
80      * to the given update values if the
81      * current reference is <tt>==</tt> to the expected reference
82      * and the current stamp is equal to the expected stamp. Any given
83      * invocation of this operation may fail (return
84      * <tt>false</tt>) spuriously, but repeated invocation when
85      * the current value holds the expected value and no other thread
86      * is also attempting to set the value will eventually succeed.
87      *
88      * @param expectedReference the expected value of the reference
89      * @param newReference the new value for the reference
90      * @param expectedStamp the expected value of the stamp
91      * @param newStamp the new value for the stamp
92      * @return true if successful
93      */

94     public boolean weakCompareAndSet(V expectedReference,
95                                      V newReference,
96                                      int expectedStamp,
97                                      int newStamp) {
98         ReferenceIntegerPair current = atomicRef.get();
99         return expectedReference == current.reference &&
100             expectedStamp == current.integer &&
101             ((newReference == current.reference &&
102               newStamp == current.integer) ||
103              atomicRef.weakCompareAndSet(current,
104                                      new ReferenceIntegerPair<V>(newReference,
105                                                               newStamp)));
106     }
107
108     /**
109      * Atomically sets the value of both the reference and stamp
110      * to the given update values if the
111      * current reference is <tt>==</tt> to the expected reference
112      * and the current stamp is equal to the expected stamp.
113      *
114      * @param expectedReference the expected value of the reference
115      * @param newReference the new value for the reference
116      * @param expectedStamp the expected value of the stamp
117      * @param newStamp the new value for the stamp
118      * @return true if successful
119      */

120     public boolean compareAndSet(V expectedReference,
121                                  V newReference,
122                                  int expectedStamp,
123                                  int newStamp) {
124         ReferenceIntegerPair current = atomicRef.get();
125         return expectedReference == current.reference &&
126             expectedStamp == current.integer &&
127             ((newReference == current.reference &&
128               newStamp == current.integer) ||
129              atomicRef.compareAndSet(current,
130                                      new ReferenceIntegerPair<V>(newReference,
131                                                               newStamp)));
132     }
133
134
135     /**
136      * Unconditionally sets the value of both the reference and stamp.
137      *
138      * @param newReference the new value for the reference
139      * @param newStamp the new value for the stamp
140      */

141     public void set(V newReference, int newStamp) {
142         ReferenceIntegerPair current = atomicRef.get();
143         if (newReference != current.reference || newStamp != current.integer)
144             atomicRef.set(new ReferenceIntegerPair<V>(newReference, newStamp));
145     }
146
147     /**
148      * Atomically sets the value of the stamp to the given update value
149      * if the current reference is <tt>==</tt> to the expected
150      * reference. Any given invocation of this operation may fail
151      * (return <tt>false</tt>) spuriously, but repeated invocation
152      * when the current value holds the expected value and no other
153      * thread is also attempting to set the value will eventually
154      * succeed.
155      *
156      * @param expectedReference the expected value of the reference
157      * @param newStamp the new value for the stamp
158      * @return true if successful
159      */

160     public boolean attemptStamp(V expectedReference, int newStamp) {
161         ReferenceIntegerPair current = atomicRef.get();
162         return expectedReference == current.reference &&
163             (newStamp == current.integer ||
164              atomicRef.compareAndSet(current,
165                                      new ReferenceIntegerPair<V>(expectedReference,
166                                                               newStamp)));
167     }
168 }
169
Popular Tags