| ||||
|
Code - Class EDU.oswego.cs.dl.util.concurrent.WaitableInt1 /* 2 File: WaitableInt.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 int 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 WaitableInt extends SynchronizedInt { 25 /** 26 * Make a new WaitableInt with the given initial value, 27 * and using its own internal lock. 28 **/ 29 public WaitableInt(int initialValue) { 30 super(initialValue); 31 } 32 33 /** 34 * Make a new WaitableInt with the given initial value, 35 * and using the supplied lock. 36 **/ 37 public WaitableInt(int initialValue, Object lock) { 38 super(initialValue, lock); 39 } 40 41 42 public int set(int newValue) { 43 synchronized (lock_) { 44 lock_.notifyAll(); 45 return super.set(newValue); 46 } 47 } 48 49 public boolean commit(int assumedValue, int newValue) { 50 synchronized (lock_) { 51 boolean success = super.commit(assumedValue, newValue); 52 if (success) lock_.notifyAll(); 53 return success; 54 } 55 } 56 57 public int increment() { 58 synchronized (lock_) { 59 lock_.notifyAll(); 60 return super.increment(); 61 } 62 } 63 64 public int decrement() { 65 synchronized (lock_) { 66 lock_.notifyAll(); 67 return super.decrement(); 68 } 69 } 70 71 public int add(int amount) { 72 synchronized (lock_) { 73 lock_.notifyAll(); 74 return super.add(amount); 75 } 76 } 77 78 public int subtract(int amount) { 79 synchronized (lock_) { 80 lock_.notifyAll(); 81 return super.subtract(amount); 82 } 83 } 84 85 public int multiply(int factor) { 86 synchronized (lock_) { 87 lock_.notifyAll(); 88 return super.multiply(factor); 89 } 90 } 91 92 public int divide(int 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 int complement() { 104 synchronized (lock_) { 105 value_ = ~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 int and(int b) { 116 synchronized (lock_) { 117 value_ = 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 int or(int b) { 128 synchronized (lock_) { 129 value_ = 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 int xor(int b) { 141 synchronized (lock_) { 142 value_ = value_ ^ b; 143 lock_.notifyAll(); 144 return value_; 145 } 146 } 147 148 149 /** 150 * Wait until value equals c, then run action if nonnull. 151 * The action is run with the synchronization lock held. 152 **/ 153 154 public void whenEqual(int c, Runnable action) throws InterruptedException { 155 synchronized(lock_) { 156 while (!(value_ == c)) lock_.wait(); 157 if (action != null) action.run(); 158 } 159 } 160 161 /** 162 * wait until value not equal to c, then run action if nonnull. 163 * The action is run with the synchronization lock held. 164 **/ 165 public void whenNotEqual(int c, Runnable action) throws InterruptedException { 166 synchronized (lock_) { 167 while (!(value_ != c)) lock_.wait(); 168 if (action != null) action.run(); 169 } 170 } 171 172 /** 173 * wait until value less than or equal to c, then run action if nonnull. 174 * The action is run with the synchronization lock held. 175 **/ 176 public void whenLessEqual(int c, Runnable action) throws InterruptedException { 177 synchronized (lock_) { 178 while (!(value_ <= c)) lock_.wait(); 179 if (action != null) action.run(); 180 } 181 } 182 183 /** 184 * wait until value less than c, then run action if nonnull. 185 * The action is run with the synchronization lock held. 186 **/ 187 public void whenLess(int c, Runnable action) throws InterruptedException { 188 synchronized (lock_) { 189 while (!(value_ < c)) lock_.wait(); 190 if (action != null) action.run(); 191 } 192 } 193 194 /** 195 * wait until value greater than or equal to c, then run action if nonnull. 196 * The action is run with the synchronization lock held. 197 **/ 198 public void whenGreaterEqual(int c, Runnable action) throws InterruptedException { 199 synchronized (lock_) { 200 while (!(value_ >= c)) lock_.wait(); 201 if (action != null) action.run(); 202 } 203 } 204 205 /** 206 * wait until value greater than c, then run action if nonnull. 207 * The action is run with the synchronization lock held. 208 **/ 209 public void whenGreater(int c, Runnable action) throws InterruptedException { 210 synchronized (lock_) { 211 while (!(value_ > c)) lock_.wait(); 212 if (action != null) action.run(); 213 } 214 } 215 216 } 217 218 |
|||
Java API By Example, From Geeks To Geeks. |
Conditions of Use |
About Us
© 2002 - 2005, KickJava.com, or its affiliates
|