| ||||
|
Code - Class EDU.oswego.cs.dl.util.concurrent.WaitableByte1 /* 2 File: WaitableByte.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 23Jun1998 dl Create public version 12 13may2004 dl Add notifying bit ops 13 */ 14 15 package EDU.oswego.cs.dl.util.concurrent; 16 17 /** 18 * A class useful for offloading waiting and signalling operations 19 * on single byte variables. 20 * <p> 21 * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>] 22 **/ 23 24 public class WaitableByte extends SynchronizedByte { 25 /** 26 * Make a new WaitableByte with the given initial value, 27 * and using its own internal lock. 28 **/ 29 public WaitableByte(byte initialValue) { 30 super(initialValue); 31 } 32 33 /** 34 * Make a new WaitableByte with the given initial value, 35 * and using the supplied lock. 36 **/ 37 public WaitableByte(byte initialValue, Object lock) { 38 super(initialValue, lock); 39 } 40 41 42 public byte set(byte newValue) { 43 synchronized (lock_) { 44 lock_.notifyAll(); 45 return super.set(newValue); 46 } 47 } 48 49 public boolean commit(byte assumedValue, byte newValue) { 50 synchronized (lock_) { 51 boolean success = super.commit(assumedValue, newValue); 52 if (success) lock_.notifyAll(); 53 return success; 54 } 55 } 56 57 public byte increment() { 58 synchronized (lock_) { 59 lock_.notifyAll(); 60 return super.increment(); 61 } 62 } 63 64 public byte decrement() { 65 synchronized (lock_) { 66 lock_.notifyAll(); 67 return super.decrement(); 68 } 69 } 70 71 public byte add(byte amount) { 72 synchronized (lock_) { 73 lock_.notifyAll(); 74 return super.add(amount); 75 } 76 } 77 78 public byte subtract(byte amount) { 79 synchronized (lock_) { 80 lock_.notifyAll(); 81 return super.subtract(amount); 82 } 83 } 84 85 public byte multiply(byte factor) { 86 synchronized (lock_) { 87 lock_.notifyAll(); 88 return super.multiply(factor); 89 } 90 } 91 92 public byte divide(byte factor) { 93 synchronized (lock_) { 94 lock_.notifyAll(); 95 return super.divide(factor); 96 } 97 } 98 99 /** 100 * Set the value to its complement 101 * @return the new value 102 **/ 103 public byte complement() { 104 synchronized (lock_) { 105 value_ = (byte)~value_; 106 lock_.notifyAll(); 107 return value_; 108 } 109 } 110 111 /** 112 * Set value to value & b. 113 * @return the new value 114 **/ 115 public byte and(byte b) { 116 synchronized (lock_) { 117 value_ = (byte)(value_ & b); 118 lock_.notifyAll(); 119 return value_; 120 } 121 } 122 123 /** 124 * Set value to value | b. 125 * @return the new value 126 **/ 127 public byte or(byte b) { 128 synchronized (lock_) { 129 value_ = (byte)(value_ | b); 130 lock_.notifyAll(); 131 return value_; 132 } 133 } 134 135 136 /** 137 * Set value to value ^ b. 138 * @return the new value 139 **/ 140 public byte xor(byte b) { 141 synchronized (lock_) { 142 value_ = (byte)(value_ ^ b); 143 lock_.notifyAll(); 144 return value_; 145 } 146 } 147 148 /** 149 * Wait until value equals c, then run action if nonnull. 150 * The action is run with the synchronization lock held. 151 **/ 152 153 public void whenEqual(byte c, Runnable action) throws InterruptedException { 154 synchronized(lock_) { 155 while (!(value_ == c)) lock_.wait(); 156 if (action != null) action.run(); 157 } 158 } 159 160 /** 161 * wait until value not equal to c, then run action if nonnull. 162 * The action is run with the synchronization lock held. 163 **/ 164 public void whenNotEqual(byte c, Runnable action) throws InterruptedException { 165 synchronized (lock_) { 166 while (!(value_ != c)) lock_.wait(); 167 if (action != null) action.run(); 168 } 169 } 170 171 /** 172 * wait until value less than or equal to c, then run action if nonnull. 173 * The action is run with the synchronization lock held. 174 **/ 175 public void whenLessEqual(byte c, Runnable action) throws InterruptedException { 176 synchronized (lock_) { 177 while (!(value_ <= c)) lock_.wait(); 178 if (action != null) action.run(); 179 } 180 } 181 182 /** 183 * wait until value less than c, then run action if nonnull. 184 * The action is run with the synchronization lock held. 185 **/ 186 public void whenLess(byte c, Runnable action) throws InterruptedException { 187 synchronized (lock_) { 188 while (!(value_ < c)) lock_.wait(); 189 if (action != null) action.run(); 190 } 191 } 192 193 /** 194 * wait until value greater than or equal to c, then run action if nonnull. 195 * The action is run with the synchronization lock held. 196 **/ 197 public void whenGreaterEqual(byte c, Runnable action) throws InterruptedException { 198 synchronized (lock_) { 199 while (!(value_ >= c)) lock_.wait(); 200 if (action != null) action.run(); 201 } 202 } 203 204 /** 205 * wait until value greater than c, then run action if nonnull. 206 * The action is run with the synchronization lock held. 207 **/ 208 public void whenGreater(byte c, Runnable action) throws InterruptedException { 209 synchronized (lock_) { 210 while (!(value_ > c)) lock_.wait(); 211 if (action != null) action.run(); 212 } 213 } 214 215 } 216 217 |
|||
Java API By Example, From Geeks To Geeks. |
Conditions of Use |
About Us
© 2002 - 2005, KickJava.com, or its affiliates
|