KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   File: WaitableRef.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   19Jun1998 dl Create public version
12 */

13
14 package EDU.oswego.cs.dl.util.concurrent;
15
16 /**
17  * A class useful for offloading synch for Object reference instance variables.
18  *
19  * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
20  **/

21
22 public class WaitableRef extends SynchronizedRef {
23
24   /**
25    * Create a WaitableRef initially holding the given reference
26    * and using its own internal lock.
27    **/

28   public WaitableRef(Object JavaDoc initialValue) {
29     super(initialValue);
30   }
31
32   /**
33    * Make a new WaitableRef with the given initial value,
34    * and using the supplied lock.
35    **/

36   public WaitableRef(Object JavaDoc initialValue, Object JavaDoc lock) {
37     super(initialValue, lock);
38   }
39
40   public Object JavaDoc set(Object JavaDoc newValue) {
41     synchronized (lock_) {
42       lock_.notifyAll();
43       return super.set(newValue);
44     }
45   }
46
47   public boolean commit(Object JavaDoc assumedValue, Object JavaDoc newValue) {
48     synchronized (lock_) {
49       boolean success = super.commit(assumedValue, newValue);
50       if (success) lock_.notifyAll();
51       return success;
52     }
53   }
54
55   /**
56    * Wait until value is null, then run action if nonnull.
57    * The action is run with the synchronization lock held.
58    **/

59
60   public void whenNull(Runnable JavaDoc action) throws InterruptedException JavaDoc {
61     synchronized (lock_) {
62       while (value_ != null) lock_.wait();
63       if (action != null) action.run();
64     }
65   }
66
67   /**
68    * wait until value is nonnull, then run action if nonnull.
69    * The action is run with the synchronization lock held.
70    **/

71   public void whenNotNull(Runnable JavaDoc action) throws InterruptedException JavaDoc {
72     synchronized (lock_) {
73       while (value_ == null) lock_.wait();
74       if (action != null) action.run();
75     }
76   }
77
78   /**
79    * Wait until value equals c, then run action if nonnull.
80    * The action is run with the synchronization lock held.
81    **/

82
83   public void whenEqual(Object JavaDoc c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
84     synchronized (lock_) {
85       while (!(value_ == c)) lock_.wait();
86       if (action != null) action.run();
87     }
88   }
89
90   /**
91    * wait until value not equal to c, then run action if nonnull.
92    * The action is run with the synchronization lock held.
93    **/

94   public void whenNotEqual(Object JavaDoc c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
95     synchronized (lock_) {
96       while (!(value_ != c)) lock_.wait();
97       if (action != null) action.run();
98     }
99   }
100
101 }
102
103
Popular Tags