Home » concurrent-sources » EDU.oswego.cs.dl.util.concurrent » [javadoc | source]

    1   /*
    2     File: WaitableBoolean.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     19Jun1998  dl               Create public version
   12   */
   13   
   14   package EDU.oswego.cs.dl.util.concurrent;
   15   
   16   /**
   17    * A class useful for offloading synch for boolean instance variables.
   18    *
   19    * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
   20    **/
   21   
   22   public class WaitableBoolean extends SynchronizedBoolean {
   23   
   24     /** Make a new WaitableBoolean with the given initial value **/
   25     public WaitableBoolean(boolean initialValue) { super(initialValue); }
   26   
   27   
   28     /** 
   29      * Make a new WaitableBoolean with the given initial value,
   30      * and using the supplied lock.
   31      **/
   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     /**
   81      * Wait until value is false, then run action if nonnull.
   82      * The action is run with the synchronization lock held.
   83      **/
   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     /**
   93      * wait until value is true, then run action if nonnull.
   94      * The action is run with the synchronization lock held.
   95      **/
   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     /**
  104      * Wait until value equals c, then run action if nonnull.
  105      * The action is run with the synchronization lock held.
  106      **/
  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     /**
  116      * wait until value not equal to c, then run action if nonnull.
  117      * The action is run with the synchronization lock held.
  118      **/
  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   

Home » concurrent-sources » EDU.oswego.cs.dl.util.concurrent » [javadoc | source]