Source Code Cross Referenced for SynchronizedRef.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: SynchronizedRef.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:          11Jun1998  dl               Create public version
012:         */
013:
014:        package EDU.oswego.cs.dl.util.concurrent;
015:
016:        import java.io.*;
017:
018:        /**
019:         * A simple class maintaining a single reference variable that
020:         * is always accessed and updated under synchronization.
021:         * <p>
022:         * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
023:         **/
024:
025:        public class SynchronizedRef extends SynchronizedVariable {
026:            /** The maintained reference **/
027:            protected Object value_;
028:
029:            /** 
030:             * Create a SynchronizedRef initially holding the given reference 
031:             * and using its own internal lock.
032:             **/
033:            public SynchronizedRef(Object initialValue) {
034:                super ();
035:                value_ = initialValue;
036:            }
037:
038:            /** 
039:             * Make a new SynchronizedRef with the given initial value,
040:             * and using the supplied lock.
041:             **/
042:            public SynchronizedRef(Object initialValue, Object lock) {
043:                super (lock);
044:                value_ = initialValue;
045:            }
046:
047:            /** 
048:             * Return the current value 
049:             **/
050:            public final Object get() {
051:                synchronized (lock_) {
052:                    return value_;
053:                }
054:            }
055:
056:            /** 
057:             * Set to newValue.
058:             * @return the old value 
059:             **/
060:
061:            public Object set(Object newValue) {
062:                synchronized (lock_) {
063:                    Object old = value_;
064:                    value_ = newValue;
065:                    return old;
066:                }
067:            }
068:
069:            /**
070:             * Set value to newValue only if it is currently assumedValue.
071:             * @return true if successful
072:             **/
073:            public boolean commit(Object assumedValue, Object newValue) {
074:                synchronized (lock_) {
075:                    boolean success = (assumedValue == value_);
076:                    if (success)
077:                        value_ = newValue;
078:                    return success;
079:                }
080:            }
081:
082:            /** 
083:             * Atomically swap values with another SynchronizedRef.
084:             * Uses identityHashCode to avoid deadlock when
085:             * two SynchronizedRefs attempt to simultaneously swap with each other.
086:             * (Note: Ordering via identyHashCode is not strictly guaranteed
087:             * by the language specification to return unique, orderable
088:             * values, but in practice JVMs rely on them being unique.)
089:             * @return the new value 
090:             **/
091:
092:            public Object swap(SynchronizedRef other) {
093:                if (other == this )
094:                    return get();
095:                SynchronizedRef fst = this ;
096:                SynchronizedRef snd = other;
097:                if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
098:                    fst = other;
099:                    snd = this;
100:                }
101:                synchronized (fst.lock_) {
102:                    synchronized (snd.lock_) {
103:                        fst.set(snd.set(fst.get()));
104:                        return get();
105:                    }
106:                }
107:            }
108:
109:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.