001: /*
002: File: WaitableChar.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: 23Jun1998 dl Create public version
012: */
013:
014: package EDU.oswego.cs.dl.util.concurrent;
015:
016: /**
017: * A class useful for offloading waiting and signalling operations
018: * on single char variables.
019: * <p>
020: * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
021: **/
022:
023: public class WaitableChar extends SynchronizedChar {
024: /**
025: * Make a new WaitableChar with the given initial value,
026: * and using its own internal lock.
027: **/
028: public WaitableChar(char initialValue) {
029: super (initialValue);
030: }
031:
032: /**
033: * Make a new WaitableChar with the given initial value,
034: * and using the supplied lock.
035: **/
036: public WaitableChar(char initialValue, Object lock) {
037: super (initialValue, lock);
038: }
039:
040: public char set(char newValue) {
041: synchronized (lock_) {
042: lock_.notifyAll();
043: return super .set(newValue);
044: }
045: }
046:
047: public boolean commit(char assumedValue, char newValue) {
048: synchronized (lock_) {
049: boolean success = super .commit(assumedValue, newValue);
050: if (success)
051: lock_.notifyAll();
052: return success;
053: }
054: }
055:
056: public char add(char amount) {
057: synchronized (lock_) {
058: lock_.notifyAll();
059: return super .add(amount);
060: }
061: }
062:
063: public char subtract(char amount) {
064: synchronized (lock_) {
065: lock_.notifyAll();
066: return super .subtract(amount);
067: }
068: }
069:
070: public char multiply(char factor) {
071: synchronized (lock_) {
072: lock_.notifyAll();
073: return super .multiply(factor);
074: }
075: }
076:
077: public char divide(char factor) {
078: synchronized (lock_) {
079: lock_.notifyAll();
080: return super .divide(factor);
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(char 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(char 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: /**
114: * wait until value less than or equal to c, then run action if nonnull.
115: * The action is run with the synchronization lock held.
116: **/
117: public void whenLessEqual(char c, Runnable action)
118: throws InterruptedException {
119: synchronized (lock_) {
120: while (!(value_ <= c))
121: lock_.wait();
122: if (action != null)
123: action.run();
124: }
125: }
126:
127: /**
128: * wait until value less than c, then run action if nonnull.
129: * The action is run with the synchronization lock held.
130: **/
131: public void whenLess(char c, Runnable action)
132: throws InterruptedException {
133: synchronized (lock_) {
134: while (!(value_ < c))
135: lock_.wait();
136: if (action != null)
137: action.run();
138: }
139: }
140:
141: /**
142: * wait until value greater than or equal to c, then run action if nonnull.
143: * The action is run with the synchronization lock held.
144: **/
145: public void whenGreaterEqual(char c, Runnable action)
146: throws InterruptedException {
147: synchronized (lock_) {
148: while (!(value_ >= c))
149: lock_.wait();
150: if (action != null)
151: action.run();
152: }
153: }
154:
155: /**
156: * wait until value greater than c, then run action if nonnull.
157: * The action is run with the synchronization lock held.
158: **/
159: public void whenGreater(char c, Runnable action)
160: throws InterruptedException {
161: synchronized (lock_) {
162: while (!(value_ > c))
163: lock_.wait();
164: if (action != null)
165: action.run();
166: }
167: }
168:
169: }
|