001: /*
002: File: WaitableBoolean.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 boolean 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 WaitableBoolean extends SynchronizedBoolean {
023:
024: /** Make a new WaitableBoolean with the given initial value **/
025: public WaitableBoolean(boolean initialValue) {
026: super (initialValue);
027: }
028:
029: /**
030: * Make a new WaitableBoolean with the given initial value,
031: * and using the supplied lock.
032: **/
033: public WaitableBoolean(boolean initialValue, Object lock) {
034: super (initialValue, lock);
035: }
036:
037: public boolean set(boolean newValue) {
038: synchronized (lock_) {
039: lock_.notifyAll();
040: return super .set(newValue);
041: }
042: }
043:
044: public boolean commit(boolean assumedValue, boolean newValue) {
045: synchronized (lock_) {
046: boolean success = super .commit(assumedValue, newValue);
047: if (success)
048: lock_.notifyAll();
049: return success;
050: }
051: }
052:
053: public boolean complement() {
054: synchronized (lock_) {
055: lock_.notifyAll();
056: return super .complement();
057: }
058: }
059:
060: public boolean and(boolean b) {
061: synchronized (lock_) {
062: lock_.notifyAll();
063: return super .and(b);
064: }
065: }
066:
067: public boolean or(boolean b) {
068: synchronized (lock_) {
069: lock_.notifyAll();
070: return super .or(b);
071: }
072: }
073:
074: public boolean xor(boolean b) {
075: synchronized (lock_) {
076: lock_.notifyAll();
077: return super .xor(b);
078: }
079: }
080:
081: /**
082: * Wait until value is false, then run action if nonnull.
083: * The action is run with the synchronization lock held.
084: **/
085:
086: public void whenFalse(Runnable action) throws InterruptedException {
087: synchronized (lock_) {
088: while (value_)
089: lock_.wait();
090: if (action != null)
091: action.run();
092: }
093: }
094:
095: /**
096: * wait until value is true, then run action if nonnull.
097: * The action is run with the synchronization lock held.
098: **/
099: public void whenTrue(Runnable action) throws InterruptedException {
100: synchronized (lock_) {
101: while (!value_)
102: lock_.wait();
103: if (action != null)
104: action.run();
105: }
106: }
107:
108: /**
109: * Wait until value equals c, then run action if nonnull.
110: * The action is run with the synchronization lock held.
111: **/
112:
113: public void whenEqual(boolean c, Runnable action)
114: throws InterruptedException {
115: synchronized (lock_) {
116: while (!(value_ == c))
117: lock_.wait();
118: if (action != null)
119: action.run();
120: }
121: }
122:
123: /**
124: * wait until value not equal to c, then run action if nonnull.
125: * The action is run with the synchronization lock held.
126: **/
127: public void whenNotEqual(boolean c, Runnable action)
128: throws InterruptedException {
129: synchronized (lock_) {
130: while (!(value_ != c))
131: lock_.wait();
132: if (action != null)
133: action.run();
134: }
135: }
136:
137: }
|