1 14 15 package EDU.oswego.cs.dl.util.concurrent; 16 17 23 24 public class WaitableInt extends SynchronizedInt { 25 29 public WaitableInt(int initialValue) { 30 super(initialValue); 31 } 32 33 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 103 public int complement() { 104 synchronized (lock_) { 105 value_ = ~value_; 106 lock_.notifyAll(); 107 return value_; 108 } 109 } 110 111 115 public int and(int b) { 116 synchronized (lock_) { 117 value_ = value_ & b; 118 lock_.notifyAll(); 119 return value_; 120 } 121 } 122 123 127 public int or(int b) { 128 synchronized (lock_) { 129 value_ = value_ | b; 130 lock_.notifyAll(); 131 return value_; 132 } 133 } 134 135 136 140 public int xor(int b) { 141 synchronized (lock_) { 142 value_ = value_ ^ b; 143 lock_.notifyAll(); 144 return value_; 145 } 146 } 147 148 149 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 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 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 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 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 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 | Popular Tags |