1 13 14 package EDU.oswego.cs.dl.util.concurrent; 15 16 21 22 public class WaitableBoolean extends SynchronizedBoolean { 23 24 25 public WaitableBoolean(boolean initialValue) { super(initialValue); } 26 27 28 32 public WaitableBoolean(boolean initialValue, Object lock) { 33 super(initialValue, lock); 34 } 35 36 37 public boolean set(boolean newValue) { 38 synchronized (lock_) { 39 lock_.notifyAll(); 40 return super.set(newValue); 41 } 42 } 43 44 public boolean commit(boolean assumedValue, boolean newValue) { 45 synchronized (lock_) { 46 boolean success = super.commit(assumedValue, newValue); 47 if (success) lock_.notifyAll(); 48 return success; 49 } 50 } 51 52 public boolean complement() { 53 synchronized (lock_) { 54 lock_.notifyAll(); 55 return super.complement(); 56 } 57 } 58 59 public boolean and(boolean b) { 60 synchronized (lock_) { 61 lock_.notifyAll(); 62 return super.and(b); 63 } 64 } 65 66 public boolean or(boolean b) { 67 synchronized (lock_) { 68 lock_.notifyAll(); 69 return super.or(b); 70 } 71 } 72 73 public boolean xor(boolean b) { 74 synchronized (lock_) { 75 lock_.notifyAll(); 76 return super.xor(b); 77 } 78 } 79 80 84 85 public void whenFalse(Runnable action) throws InterruptedException { 86 synchronized (lock_) { 87 while (value_) lock_.wait(); 88 if (action != null) action.run(); 89 } 90 } 91 92 96 public void whenTrue(Runnable action) throws InterruptedException { 97 synchronized (lock_) { 98 while (!value_) lock_.wait(); 99 if (action != null) action.run(); 100 } 101 } 102 103 107 108 public void whenEqual(boolean c, Runnable action) throws InterruptedException { 109 synchronized (lock_) { 110 while (!(value_ == c)) lock_.wait(); 111 if (action != null) action.run(); 112 } 113 } 114 115 119 public void whenNotEqual(boolean c, Runnable action) throws InterruptedException { 120 synchronized (lock_) { 121 while (!(value_ != c)) lock_.wait(); 122 if (action != null) action.run(); 123 } 124 } 125 126 } 127 128 | Popular Tags |