Source Code Cross Referenced for AtomicStampedReference.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » concurrent » atomic » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util.concurrent.atomic 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
003         *
004         * This code is free software; you can redistribute it and/or modify it
005         * under the terms of the GNU General Public License version 2 only, as
006         * published by the Free Software Foundation.  Sun designates this
007         * particular file as subject to the "Classpath" exception as provided
008         * by Sun in the LICENSE file that accompanied this code.
009         *
010         * This code is distributed in the hope that it will be useful, but WITHOUT
011         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
013         * version 2 for more details (a copy is included in the LICENSE file that
014         * accompanied this code).
015         *
016         * You should have received a copy of the GNU General Public License version
017         * 2 along with this work; if not, write to the Free Software Foundation,
018         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
019         *
020         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
021         * CA 95054 USA or visit www.sun.com if you need additional information or
022         * have any questions.
023         */
024
025        /*
026         * This file is available under and governed by the GNU General Public
027         * License version 2 only, as published by the Free Software Foundation.
028         * However, the following notice accompanied the original version of this
029         * file:
030         *
031         * Written by Doug Lea with assistance from members of JCP JSR-166
032         * Expert Group and released to the public domain, as explained at
033         * http://creativecommons.org/licenses/publicdomain
034         */
035
036        package java.util.concurrent.atomic;
037
038        /**
039         * An {@code AtomicStampedReference} maintains an object reference
040         * along with an integer "stamp", that can be updated atomically.
041         *
042         * <p> Implementation note. This implementation maintains stamped
043         * references by creating internal objects representing "boxed"
044         * [reference, integer] pairs.
045         *
046         * @since 1.5
047         * @author Doug Lea
048         * @param <V> The type of object referred to by this reference
049         */
050        public class AtomicStampedReference<V> {
051
052            private static class ReferenceIntegerPair<T> {
053                private final T reference;
054                private final int integer;
055
056                ReferenceIntegerPair(T r, int i) {
057                    reference = r;
058                    integer = i;
059                }
060            }
061
062            private final AtomicReference<ReferenceIntegerPair<V>> atomicRef;
063
064            /**
065             * Creates a new {@code AtomicStampedReference} with the given
066             * initial values.
067             *
068             * @param initialRef the initial reference
069             * @param initialStamp the initial stamp
070             */
071            public AtomicStampedReference(V initialRef, int initialStamp) {
072                atomicRef = new AtomicReference<ReferenceIntegerPair<V>>(
073                        new ReferenceIntegerPair<V>(initialRef, initialStamp));
074            }
075
076            /**
077             * Returns the current value of the reference.
078             *
079             * @return the current value of the reference
080             */
081            public V getReference() {
082                return atomicRef.get().reference;
083            }
084
085            /**
086             * Returns the current value of the stamp.
087             *
088             * @return the current value of the stamp
089             */
090            public int getStamp() {
091                return atomicRef.get().integer;
092            }
093
094            /**
095             * Returns the current values of both the reference and the stamp.
096             * Typical usage is {@code int[1] holder; ref = v.get(holder); }.
097             *
098             * @param stampHolder an array of size of at least one.  On return,
099             * {@code stampholder[0]} will hold the value of the stamp.
100             * @return the current value of the reference
101             */
102            public V get(int[] stampHolder) {
103                ReferenceIntegerPair<V> p = atomicRef.get();
104                stampHolder[0] = p.integer;
105                return p.reference;
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 {@code ==} to the expected reference
112             * and the current stamp is equal to the expected stamp.
113             *
114             * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
115             * and does not provide ordering guarantees, so is only rarely an
116             * appropriate alternative to {@code compareAndSet}.
117             *
118             * @param expectedReference the expected value of the reference
119             * @param newReference the new value for the reference
120             * @param expectedStamp the expected value of the stamp
121             * @param newStamp the new value for the stamp
122             * @return true if successful
123             */
124            public boolean weakCompareAndSet(V expectedReference,
125                    V newReference, int expectedStamp, int newStamp) {
126                ReferenceIntegerPair<V> current = atomicRef.get();
127                return expectedReference == current.reference
128                        && expectedStamp == current.integer
129                        && ((newReference == current.reference && newStamp == current.integer) || atomicRef
130                                .weakCompareAndSet(current,
131                                        new ReferenceIntegerPair<V>(
132                                                newReference, newStamp)));
133            }
134
135            /**
136             * Atomically sets the value of both the reference and stamp
137             * to the given update values if the
138             * current reference is {@code ==} to the expected reference
139             * and the current stamp is equal to the expected stamp.
140             *
141             * @param expectedReference the expected value of the reference
142             * @param newReference the new value for the reference
143             * @param expectedStamp the expected value of the stamp
144             * @param newStamp the new value for the stamp
145             * @return true if successful
146             */
147            public boolean compareAndSet(V expectedReference, V newReference,
148                    int expectedStamp, int newStamp) {
149                ReferenceIntegerPair<V> current = atomicRef.get();
150                return expectedReference == current.reference
151                        && expectedStamp == current.integer
152                        && ((newReference == current.reference && newStamp == current.integer) || atomicRef
153                                .compareAndSet(current,
154                                        new ReferenceIntegerPair<V>(
155                                                newReference, newStamp)));
156            }
157
158            /**
159             * Unconditionally sets the value of both the reference and stamp.
160             *
161             * @param newReference the new value for the reference
162             * @param newStamp the new value for the stamp
163             */
164            public void set(V newReference, int newStamp) {
165                ReferenceIntegerPair<V> current = atomicRef.get();
166                if (newReference != current.reference
167                        || newStamp != current.integer)
168                    atomicRef.set(new ReferenceIntegerPair<V>(newReference,
169                            newStamp));
170            }
171
172            /**
173             * Atomically sets the value of the stamp to the given update value
174             * if the current reference is {@code ==} to the expected
175             * reference.  Any given invocation of this operation may fail
176             * (return {@code false}) spuriously, but repeated invocation
177             * when the current value holds the expected value and no other
178             * thread is also attempting to set the value will eventually
179             * succeed.
180             *
181             * @param expectedReference the expected value of the reference
182             * @param newStamp the new value for the stamp
183             * @return true if successful
184             */
185            public boolean attemptStamp(V expectedReference, int newStamp) {
186                ReferenceIntegerPair<V> current = atomicRef.get();
187                return expectedReference == current.reference
188                        && (newStamp == current.integer || atomicRef
189                                .compareAndSet(current,
190                                        new ReferenceIntegerPair<V>(
191                                                expectedReference, newStamp)));
192            }
193        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.