Source Code Cross Referenced for AtomicReference.java in  » Apache-Harmony-Java-SE » java-package » java » util » concurrent » atomic » Java Source Code / Java DocumentationJava Source Code and Java Documentation

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 geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » java package » java.util.concurrent.atomic 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Written by Doug Lea with assistance from members of JCP JSR-166
003:         * Expert Group and released to the public domain, as explained at
004:         * http://creativecommons.org/licenses/publicdomain
005:         */
006:
007:        package java.util.concurrent.atomic;
008:
009:        import sun.misc.Unsafe;
010:
011:        /**
012:         * An object reference that may be updated atomically. See the {@link
013:         * java.util.concurrent.atomic} package specification for description
014:         * of the properties of atomic variables.
015:         * @since 1.5
016:         * @author Doug Lea
017:         * @param <V> The type of object referred to by this reference
018:         */
019:        public class AtomicReference<V> implements  java.io.Serializable {
020:            private static final long serialVersionUID = -1848883965231344442L;
021:
022:            private static final Unsafe unsafe = Unsafe.getUnsafe();
023:            private static final long valueOffset;
024:
025:            static {
026:                try {
027:                    valueOffset = unsafe
028:                            .objectFieldOffset(AtomicReference.class
029:                                    .getDeclaredField("value"));
030:                } catch (Exception ex) {
031:                    throw new Error(ex);
032:                }
033:            }
034:
035:            private volatile V value;
036:
037:            /**
038:             * Create a new AtomicReference with the given initial value.
039:             *
040:             * @param initialValue the initial value
041:             */
042:            public AtomicReference(V initialValue) {
043:                value = initialValue;
044:            }
045:
046:            /**
047:             * Create a new AtomicReference with null initial value.
048:             */
049:            public AtomicReference() {
050:            }
051:
052:            /**
053:             * Get the current value.
054:             *
055:             * @return the current value
056:             */
057:            public final V get() {
058:                return value;
059:            }
060:
061:            /**
062:             * Set to the given value.
063:             *
064:             * @param newValue the new value
065:             */
066:            public final void set(V newValue) {
067:                value = newValue;
068:            }
069:
070:            /**
071:             * Atomically set the value to the given updated value
072:             * if the current value <tt>==</tt> the expected value.
073:             * @param expect the expected value
074:             * @param update the new value
075:             * @return true if successful. False return indicates that
076:             * the actual value was not equal to the expected value.
077:             */
078:            public final boolean compareAndSet(V expect, V update) {
079:                return unsafe.compareAndSwapObject(this , valueOffset, expect,
080:                        update);
081:            }
082:
083:            /**
084:             * Atomically set the value to the given updated value
085:             * if the current value <tt>==</tt> the expected value.
086:             * May fail spuriously.
087:             * @param expect the expected value
088:             * @param update the new value
089:             * @return true if successful.
090:             */
091:            public final boolean weakCompareAndSet(V expect, V update) {
092:                return unsafe.compareAndSwapObject(this , valueOffset, expect,
093:                        update);
094:            }
095:
096:            /**
097:             * Set to the given value and return the old value.
098:             *
099:             * @param newValue the new value
100:             * @return the previous value
101:             */
102:            public final V getAndSet(V newValue) {
103:                while (true) {
104:                    V x = get();
105:                    if (compareAndSet(x, newValue))
106:                        return x;
107:                }
108:            }
109:
110:            /**
111:             * Returns the String representation of the current value.
112:             * @return the String representation of the current value.
113:             */
114:            public String toString() {
115:                return String.valueOf(get());
116:            }
117:
118:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.