001: /*
002: File: WaitableLong.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 long 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 WaitableLong extends SynchronizedLong {
024: /**
025: * Make a new WaitableLong with the given initial value,
026: * and using its own internal lock.
027: **/
028: public WaitableLong(long initialValue) {
029: super (initialValue);
030: }
031:
032: /**
033: * Make a new WaitableLong with the given initial value,
034: * and using the supplied lock.
035: **/
036: public WaitableLong(long initialValue, Object lock) {
037: super (initialValue, lock);
038: }
039:
040: public long set(long newValue) {
041: synchronized (lock_) {
042: lock_.notifyAll();
043: return super .set(newValue);
044: }
045: }
046:
047: public boolean commit(long assumedValue, long 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 long increment() {
057: synchronized (lock_) {
058: lock_.notifyAll();
059: return super .increment();
060: }
061: }
062:
063: public long decrement() {
064: synchronized (lock_) {
065: lock_.notifyAll();
066: return super .decrement();
067: }
068: }
069:
070: public long add(long amount) {
071: synchronized (lock_) {
072: lock_.notifyAll();
073: return super .add(amount);
074: }
075: }
076:
077: public long subtract(long amount) {
078: synchronized (lock_) {
079: lock_.notifyAll();
080: return super .subtract(amount);
081: }
082: }
083:
084: public long multiply(long factor) {
085: synchronized (lock_) {
086: lock_.notifyAll();
087: return super .multiply(factor);
088: }
089: }
090:
091: public long divide(long factor) {
092: synchronized (lock_) {
093: lock_.notifyAll();
094: return super .divide(factor);
095: }
096: }
097:
098: /**
099: * Wait until value equals c, then run action if nonnull.
100: * The action is run with the synchronization lock held.
101: **/
102:
103: public void whenEqual(long 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 not equal to c, then run action if nonnull.
115: * The action is run with the synchronization lock held.
116: **/
117: public void whenNotEqual(long 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 or equal to c, then run action if nonnull.
129: * The action is run with the synchronization lock held.
130: **/
131: public void whenLessEqual(long 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 less than c, then run action if nonnull.
143: * The action is run with the synchronization lock held.
144: **/
145: public void whenLess(long 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 or equal to c, then run action if nonnull.
157: * The action is run with the synchronization lock held.
158: **/
159: public void whenGreaterEqual(long 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: /**
170: * wait until value greater than c, then run action if nonnull.
171: * The action is run with the synchronization lock held.
172: **/
173: public void whenGreater(long c, Runnable action)
174: throws InterruptedException {
175: synchronized (lock_) {
176: while (!(value_ > c))
177: lock_.wait();
178: if (action != null)
179: action.run();
180: }
181: }
182:
183: }
|