KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > EDU > oswego > cs > dl > util > concurrent > SynchronizedRef


1 /*
2   File: SynchronizedRef.java
3
4   Originally written by Doug Lea and released into the public domain.
5   This may be used for any purposes whatsoever without acknowledgment.
6   Thanks for the assistance and support of Sun Microsystems Labs,
7   and everyone contributing, testing, and using this code.
8
9   History:
10   Date Who What
11   11Jun1998 dl Create public version
12 */

13
14 package EDU.oswego.cs.dl.util.concurrent;
15 import java.io.*;
16
17 /**
18  * A simple class maintaining a single reference variable that
19  * is always accessed and updated under synchronization.
20  * <p>
21  * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
22  **/

23
24 public class SynchronizedRef extends SynchronizedVariable {
25   /** The maintained reference **/
26   protected Object JavaDoc value_;
27
28   /**
29    * Create a SynchronizedRef initially holding the given reference
30    * and using its own internal lock.
31    **/

32   public SynchronizedRef(Object JavaDoc initialValue) {
33     super();
34     value_ = initialValue;
35   }
36
37   /**
38    * Make a new SynchronizedRef with the given initial value,
39    * and using the supplied lock.
40    **/

41   public SynchronizedRef(Object JavaDoc initialValue, Object JavaDoc lock) {
42     super(lock);
43     value_ = initialValue;
44   }
45
46   /**
47    * Return the current value
48    **/

49   public final Object JavaDoc get() { synchronized(lock_) { return value_; } }
50
51   /**
52    * Set to newValue.
53    * @return the old value
54    **/

55
56   public Object JavaDoc set(Object JavaDoc newValue) {
57     synchronized (lock_) {
58       Object JavaDoc old = value_;
59       value_ = newValue;
60       return old;
61     }
62   }
63
64   /**
65    * Set value to newValue only if it is currently assumedValue.
66    * @return true if successful
67    **/

68   public boolean commit(Object JavaDoc assumedValue, Object JavaDoc newValue) {
69     synchronized(lock_) {
70       boolean success = (assumedValue == value_);
71       if (success) value_ = newValue;
72       return success;
73     }
74   }
75
76
77   /**
78    * Atomically swap values with another SynchronizedRef.
79    * Uses identityHashCode to avoid deadlock when
80    * two SynchronizedRefs attempt to simultaneously swap with each other.
81    * (Note: Ordering via identyHashCode is not strictly guaranteed
82    * by the language specification to return unique, orderable
83    * values, but in practice JVMs rely on them being unique.)
84    * @return the new value
85    **/

86
87   public Object JavaDoc swap(SynchronizedRef other) {
88     if (other == this) return get();
89     SynchronizedRef fst = this;
90     SynchronizedRef snd = other;
91     if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
92       fst = other;
93       snd = this;
94     }
95     synchronized(fst.lock_) {
96       synchronized(snd.lock_) {
97         fst.set(snd.set(fst.get()));
98         return get();
99       }
100     }
101   }
102
103
104 }
105
Popular Tags