Source Code Cross Referenced for SynchronizedDouble.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: SynchronizedDouble.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 double 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 SynchronizedDouble extends SynchronizedVariable implements 
023:                Comparable, Cloneable {
024:
025:            protected double value_;
026:
027:            /** 
028:             * Make a new SynchronizedDouble with the given initial value,
029:             * and using its own internal lock.
030:             **/
031:            public SynchronizedDouble(double initialValue) {
032:                super ();
033:                value_ = initialValue;
034:            }
035:
036:            /** 
037:             * Make a new SynchronizedDouble with the given initial value,
038:             * and using the supplied lock.
039:             **/
040:            public SynchronizedDouble(double initialValue, Object lock) {
041:                super (lock);
042:                value_ = initialValue;
043:            }
044:
045:            /** 
046:             * Return the current value 
047:             **/
048:            public final double get() {
049:                synchronized (lock_) {
050:                    return value_;
051:                }
052:            }
053:
054:            /** 
055:             * Set to newValue.
056:             * @return the old value 
057:             **/
058:
059:            public double set(double newValue) {
060:                synchronized (lock_) {
061:                    double old = value_;
062:                    value_ = newValue;
063:                    return old;
064:                }
065:            }
066:
067:            /**
068:             * Set value to newValue only if it is currently assumedValue.
069:             * @return true if successful
070:             **/
071:            public boolean commit(double assumedValue, double newValue) {
072:                synchronized (lock_) {
073:                    boolean success = (assumedValue == value_);
074:                    if (success)
075:                        value_ = newValue;
076:                    return success;
077:                }
078:            }
079:
080:            /** 
081:             * Atomically swap values with another SynchronizedDouble.
082:             * Uses identityHashCode to avoid deadlock when
083:             * two SynchronizedDoubles attempt to simultaneously swap with each other.
084:             * (Note: Ordering via identyHashCode is not strictly guaranteed
085:             * by the language specification to return unique, orderable
086:             * values, but in practice JVMs rely on them being unique.)
087:             * @return the new value 
088:             **/
089:
090:            public double swap(SynchronizedDouble other) {
091:                if (other == this )
092:                    return get();
093:                SynchronizedDouble fst = this ;
094:                SynchronizedDouble snd = other;
095:                if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
096:                    fst = other;
097:                    snd = this ;
098:                }
099:                synchronized (fst.lock_) {
100:                    synchronized (snd.lock_) {
101:                        fst.set(snd.set(fst.get()));
102:                        return get();
103:                    }
104:                }
105:            }
106:
107:            /** 
108:             * Add amount to value (i.e., set value += amount)
109:             * @return the new value 
110:             **/
111:            public double add(double amount) {
112:                synchronized (lock_) {
113:                    return value_ += amount;
114:                }
115:            }
116:
117:            /** 
118:             * Subtract amount from value (i.e., set value -= amount)
119:             * @return the new value 
120:             **/
121:            public double subtract(double amount) {
122:                synchronized (lock_) {
123:                    return value_ -= amount;
124:                }
125:            }
126:
127:            /** 
128:             * Multiply value by factor (i.e., set value *= factor)
129:             * @return the new value 
130:             **/
131:            public synchronized double multiply(double factor) {
132:                synchronized (lock_) {
133:                    return value_ *= factor;
134:                }
135:            }
136:
137:            /** 
138:             * Divide value by factor (i.e., set value /= factor)
139:             * @return the new value 
140:             **/
141:            public double divide(double factor) {
142:                synchronized (lock_) {
143:                    return value_ /= factor;
144:                }
145:            }
146:
147:            public int compareTo(double other) {
148:                double val = get();
149:                return (val < other) ? -1 : (val == other) ? 0 : 1;
150:            }
151:
152:            public int compareTo(SynchronizedDouble other) {
153:                return compareTo(other.get());
154:            }
155:
156:            public int compareTo(Object other) {
157:                return compareTo((SynchronizedDouble) other);
158:            }
159:
160:            public boolean equals(Object other) {
161:                if (other != null && other instanceof  SynchronizedDouble)
162:                    return get() == ((SynchronizedDouble) other).get();
163:                else
164:                    return false;
165:            }
166:
167:            public int hashCode() { // same hash as Double
168:                long bits = Double.doubleToLongBits(get());
169:                return (int) (bits ^ (bits >> 32));
170:            }
171:
172:            public String toString() {
173:                return String.valueOf(get());
174:            }
175:
176:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.