Source Code Cross Referenced for AtomicBoolean.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:         * A <tt>boolean</tt> value that may be updated atomically. See the
013:         * {@link java.util.concurrent.atomic} package specification for
014:         * description of the properties of atomic variables. An
015:         * <tt>AtomicBoolean</tt> is used in applications such as atomically
016:         * updated flags, and cannot be used as a replacement for a
017:         * {@link java.lang.Boolean}.
018:         *
019:         * @since 1.5
020:         * @author Doug Lea
021:         */
022:        public class AtomicBoolean implements  java.io.Serializable {
023:            private static final long serialVersionUID = 4654671469794556979L;
024:            // setup to use Unsafe.compareAndSwapInt for updates
025:            private static final Unsafe unsafe = Unsafe.getUnsafe();
026:            private static final long valueOffset;
027:
028:            static {
029:                try {
030:                    valueOffset = unsafe.objectFieldOffset(AtomicBoolean.class
031:                            .getDeclaredField("value"));
032:                } catch (Exception ex) {
033:                    throw new Error(ex);
034:                }
035:            }
036:
037:            private volatile int value;
038:
039:            /**
040:             * Creates a new <tt>AtomicBoolean</tt> with the given initial value.
041:             *
042:             * @param initialValue the initial value
043:             */
044:            public AtomicBoolean(boolean initialValue) {
045:                value = initialValue ? 1 : 0;
046:            }
047:
048:            /**
049:             * Creates a new <tt>AtomicBoolean</tt> with initial value <tt>false</tt>.
050:             */
051:            public AtomicBoolean() {
052:            }
053:
054:            /**
055:             * Returns the current value.
056:             *
057:             * @return the current value
058:             */
059:            public final boolean get() {
060:                return value != 0;
061:            }
062:
063:            /**
064:             * Atomically sets the value to the given update value if the
065:             * current value is equal to the expected value.  Any given
066:             * invocation of this operation may fail (return
067:             * <tt>false</tt>) spuriously, but repeated invocation when
068:             * the current value holds the expected value and no other thread
069:             * is also attempting to set the value will eventually succeed.
070:             *
071:             * @param expect the expected value
072:             * @param update the new value
073:             * @return true if successful
074:             */
075:            public final boolean compareAndSet(boolean expect, boolean update) {
076:                int e = expect ? 1 : 0;
077:                int u = update ? 1 : 0;
078:                return unsafe.compareAndSwapInt(this , valueOffset, e, u);
079:            }
080:
081:            /**
082:             * Atomically set the value to the given updated value
083:             * if the current value <tt>==</tt> the expected value.
084:             * May fail spuriously.
085:             * @param expect the expected value
086:             * @param update the new value
087:             * @return true if successful.
088:             */
089:            public boolean weakCompareAndSet(boolean expect, boolean update) {
090:                int e = expect ? 1 : 0;
091:                int u = update ? 1 : 0;
092:                return unsafe.compareAndSwapInt(this , valueOffset, e, u);
093:            }
094:
095:            /**
096:             * Unconditionally sets to the given value.
097:             *
098:             * @param newValue the new value
099:             */
100:            public final void set(boolean newValue) {
101:                value = newValue ? 1 : 0;
102:            }
103:
104:            /**
105:             * Sets to the given value and returns the previous value.
106:             *
107:             * @param newValue the new value
108:             * @return the previous value
109:             */
110:            public final boolean getAndSet(boolean newValue) {
111:                for (;;) {
112:                    boolean current = get();
113:                    if (compareAndSet(current, newValue))
114:                        return current;
115:                }
116:            }
117:
118:            /**
119:             * Returns the String representation of the current value.
120:             * @return the String representation of the current value.
121:             */
122:            public String toString() {
123:                return Boolean.toString(get());
124:            }
125:
126:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.