Source Code Cross Referenced for SynchronizedBoolean.java in  » Ajax » Laszlo-4.0.10 » EDU » oswego » cs » dl » util » concurrent » 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 » Ajax » Laszlo 4.0.10 » EDU.oswego.cs.dl.util.concurrent 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:          File: SynchronizedBoolean.java
003:
004:          Originally written by Doug Lea and released into the public domain.
005:          This may be used for any purposes whatsoever without acknowledgment.
006:          Thanks for the assistance and support of Sun Microsystems Labs,
007:          and everyone contributing, testing, and using this code.
008:
009:          History:
010:          Date       Who                What
011:          19Jun1998  dl               Create public version
012:         */
013:
014:        package EDU.oswego.cs.dl.util.concurrent;
015:
016:        /**
017:         * A class useful for offloading synch for boolean instance variables.
018:         *
019:         * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
020:         **/
021:
022:        public class SynchronizedBoolean extends SynchronizedVariable implements 
023:                Comparable, Cloneable {
024:            protected boolean value_;
025:
026:            /** 
027:             * Make a new SynchronizedBoolean with the given initial value,
028:             * and using its own internal lock.
029:             **/
030:            public SynchronizedBoolean(boolean initialValue) {
031:                super ();
032:                value_ = initialValue;
033:            }
034:
035:            /** 
036:             * Make a new SynchronizedBoolean with the given initial value,
037:             * and using the supplied lock.
038:             **/
039:            public SynchronizedBoolean(boolean initialValue, Object lock) {
040:                super (lock);
041:                value_ = initialValue;
042:            }
043:
044:            /** 
045:             * Return the current value 
046:             **/
047:            public final boolean get() {
048:                synchronized (lock_) {
049:                    return value_;
050:                }
051:            }
052:
053:            /** 
054:             * Set to newValue.
055:             * @return the old value 
056:             **/
057:
058:            public boolean set(boolean newValue) {
059:                synchronized (lock_) {
060:                    boolean old = value_;
061:                    value_ = newValue;
062:                    return old;
063:                }
064:            }
065:
066:            /**
067:             * Set value to newValue only if it is currently assumedValue.
068:             * @return true if successful
069:             **/
070:            public boolean commit(boolean assumedValue, boolean newValue) {
071:                synchronized (lock_) {
072:                    boolean success = (assumedValue == value_);
073:                    if (success)
074:                        value_ = newValue;
075:                    return success;
076:                }
077:            }
078:
079:            /** 
080:             * Atomically swap values with another SynchronizedBoolean.
081:             * Uses identityHashCode to avoid deadlock when
082:             * two SynchronizedBooleans attempt to simultaneously swap with each other.
083:             * (Note: Ordering via identyHashCode is not strictly guaranteed
084:             * by the language specification to return unique, orderable
085:             * values, but in practice JVMs rely on them being unique.)
086:             * @return the new value 
087:             **/
088:
089:            public boolean swap(SynchronizedBoolean other) {
090:                if (other == this )
091:                    return get();
092:                SynchronizedBoolean fst = this ;
093:                SynchronizedBoolean snd = other;
094:                if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
095:                    fst = other;
096:                    snd = this ;
097:                }
098:                synchronized (fst.lock_) {
099:                    synchronized (snd.lock_) {
100:                        fst.set(snd.set(fst.get()));
101:                        return get();
102:                    }
103:                }
104:            }
105:
106:            /** 
107:             * Set the value to its complement
108:             * @return the new value 
109:             **/
110:            public boolean complement() {
111:                synchronized (lock_) {
112:                    value_ = !value_;
113:                    return value_;
114:                }
115:            }
116:
117:            /** 
118:             * Set value to value &amp; b.
119:             * @return the new value 
120:             **/
121:            public boolean and(boolean b) {
122:                synchronized (lock_) {
123:                    value_ = value_ & b;
124:                    return value_;
125:                }
126:            }
127:
128:            /** 
129:             * Set value to value | b.
130:             * @return the new value 
131:             **/
132:            public boolean or(boolean b) {
133:                synchronized (lock_) {
134:                    value_ = value_ | b;
135:                    return value_;
136:                }
137:            }
138:
139:            /** 
140:             * Set value to value ^ b.
141:             * @return the new value 
142:             **/
143:            public boolean xor(boolean b) {
144:                synchronized (lock_) {
145:                    value_ = value_ ^ b;
146:                    return value_;
147:                }
148:            }
149:
150:            public int compareTo(boolean other) {
151:                boolean val = get();
152:                return (val == other) ? 0 : (val) ? 1 : -1;
153:            }
154:
155:            public int compareTo(SynchronizedBoolean other) {
156:                return compareTo(other.get());
157:            }
158:
159:            public int compareTo(Object other) {
160:                return compareTo((SynchronizedBoolean) other);
161:            }
162:
163:            public boolean equals(Object other) {
164:                if (other != null && other instanceof  SynchronizedBoolean)
165:                    return get() == ((SynchronizedBoolean) other).get();
166:                else
167:                    return false;
168:            }
169:
170:            public int hashCode() {
171:                boolean b = get();
172:                return (b) ? 3412688 : 8319343; // entirely arbitrary
173:            }
174:
175:            public String toString() {
176:                return String.valueOf(get());
177:            }
178:
179:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.