1 13 14 package EDU.oswego.cs.dl.util.concurrent; 15 16 21 22 public class WaitableRef extends SynchronizedRef { 23 24 28 public WaitableRef(Object initialValue) { 29 super(initialValue); 30 } 31 32 36 public WaitableRef(Object initialValue, Object lock) { 37 super(initialValue, lock); 38 } 39 40 public Object set(Object newValue) { 41 synchronized (lock_) { 42 lock_.notifyAll(); 43 return super.set(newValue); 44 } 45 } 46 47 public boolean commit(Object assumedValue, Object newValue) { 48 synchronized (lock_) { 49 boolean success = super.commit(assumedValue, newValue); 50 if (success) lock_.notifyAll(); 51 return success; 52 } 53 } 54 55 59 60 public void whenNull(Runnable action) throws InterruptedException { 61 synchronized (lock_) { 62 while (value_ != null) lock_.wait(); 63 if (action != null) action.run(); 64 } 65 } 66 67 71 public void whenNotNull(Runnable action) throws InterruptedException { 72 synchronized (lock_) { 73 while (value_ == null) lock_.wait(); 74 if (action != null) action.run(); 75 } 76 } 77 78 82 83 public void whenEqual(Object c, Runnable action) throws InterruptedException { 84 synchronized (lock_) { 85 while (!(value_ == c)) lock_.wait(); 86 if (action != null) action.run(); 87 } 88 } 89 90 94 public void whenNotEqual(Object c, Runnable action) throws InterruptedException { 95 synchronized (lock_) { 96 while (!(value_ != c)) lock_.wait(); 97 if (action != null) action.run(); 98 } 99 } 100 101 } 102 103 | Popular Tags |