1 13 14 package EDU.oswego.cs.dl.util.concurrent; 15 16 22 23 public class WaitableDouble extends SynchronizedDouble { 24 28 public WaitableDouble(double initialValue) { 29 super(initialValue); 30 } 31 32 36 public WaitableDouble(double initialValue, Object lock) { 37 super(initialValue, lock); 38 } 39 40 41 public double set(double newValue) { 42 synchronized (lock_) { 43 lock_.notifyAll(); 44 return super.set(newValue); 45 } 46 } 47 48 public boolean commit(double assumedValue, double newValue) { 49 synchronized (lock_) { 50 boolean success = super.commit(assumedValue, newValue); 51 if (success) lock_.notifyAll(); 52 return success; 53 } 54 } 55 56 57 public double add(double amount) { 58 synchronized (lock_) { 59 lock_.notifyAll(); 60 return super.add(amount); 61 } 62 } 63 64 public double subtract(double amount) { 65 synchronized (lock_) { 66 lock_.notifyAll(); 67 return super.subtract(amount); 68 } 69 } 70 71 public double multiply(double factor) { 72 synchronized (lock_) { 73 lock_.notifyAll(); 74 return super.multiply(factor); 75 } 76 } 77 78 public double divide(double factor) { 79 synchronized (lock_) { 80 lock_.notifyAll(); 81 return super.divide(factor); 82 } 83 } 84 85 86 90 91 public void whenEqual(double c, Runnable action) throws InterruptedException { 92 synchronized(lock_) { 93 while (!(value_ == c)) lock_.wait(); 94 if (action != null) action.run(); 95 } 96 } 97 98 102 public void whenNotEqual(double c, Runnable action) throws InterruptedException { 103 synchronized (lock_) { 104 while (!(value_ != c)) lock_.wait(); 105 if (action != null) action.run(); 106 } 107 } 108 109 113 public void whenLessEqual(double c, Runnable action) throws InterruptedException { 114 synchronized (lock_) { 115 while (!(value_ <= c)) lock_.wait(); 116 if (action != null) action.run(); 117 } 118 } 119 120 124 public void whenLess(double c, Runnable action) throws InterruptedException { 125 synchronized (lock_) { 126 while (!(value_ < c)) lock_.wait(); 127 if (action != null) action.run(); 128 } 129 } 130 131 135 public void whenGreaterEqual(double c, Runnable action) throws InterruptedException { 136 synchronized (lock_) { 137 while (!(value_ >= c)) lock_.wait(); 138 if (action != null) action.run(); 139 } 140 } 141 142 146 public void whenGreater(double c, Runnable action) throws InterruptedException { 147 synchronized (lock_) { 148 while (!(value_ > c)) lock_.wait(); 149 if (action != null) action.run(); 150 } 151 } 152 153 } 154 155 | Popular Tags |