001: /*
002: File: WaitableRef.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 Object reference 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 WaitableRef extends SynchronizedRef {
023:
024: /**
025: * Create a WaitableRef initially holding the given reference
026: * and using its own internal lock.
027: **/
028: public WaitableRef(Object initialValue) {
029: super (initialValue);
030: }
031:
032: /**
033: * Make a new WaitableRef with the given initial value,
034: * and using the supplied lock.
035: **/
036: public WaitableRef(Object initialValue, Object lock) {
037: super (initialValue, lock);
038: }
039:
040: public Object set(Object newValue) {
041: synchronized (lock_) {
042: lock_.notifyAll();
043: return super .set(newValue);
044: }
045: }
046:
047: public boolean commit(Object assumedValue, Object newValue) {
048: synchronized (lock_) {
049: boolean success = super .commit(assumedValue, newValue);
050: if (success)
051: lock_.notifyAll();
052: return success;
053: }
054: }
055:
056: /**
057: * Wait until value is null, then run action if nonnull.
058: * The action is run with the synchronization lock held.
059: **/
060:
061: public void whenNull(Runnable action) throws InterruptedException {
062: synchronized (lock_) {
063: while (value_ != null)
064: lock_.wait();
065: if (action != null)
066: action.run();
067: }
068: }
069:
070: /**
071: * wait until value is nonnull, then run action if nonnull.
072: * The action is run with the synchronization lock held.
073: **/
074: public void whenNotNull(Runnable action)
075: throws InterruptedException {
076: synchronized (lock_) {
077: while (value_ == null)
078: lock_.wait();
079: if (action != null)
080: action.run();
081: }
082: }
083:
084: /**
085: * Wait until value equals c, then run action if nonnull.
086: * The action is run with the synchronization lock held.
087: **/
088:
089: public void whenEqual(Object c, Runnable action)
090: throws InterruptedException {
091: synchronized (lock_) {
092: while (!(value_ == c))
093: lock_.wait();
094: if (action != null)
095: action.run();
096: }
097: }
098:
099: /**
100: * wait until value not equal to c, then run action if nonnull.
101: * The action is run with the synchronization lock held.
102: **/
103: public void whenNotEqual(Object c, Runnable action)
104: throws InterruptedException {
105: synchronized (lock_) {
106: while (!(value_ != c))
107: lock_.wait();
108: if (action != null)
109: action.run();
110: }
111: }
112:
113: }
|