Class CondVar


  • public class CondVar
    extends java.lang.Object
    This class is designed for fans of POSIX pthreads programming. If you restrict yourself to Mutexes and CondVars, you can use most of your favorite constructions. Don't randomly mix them with synchronized methods or blocks though.

    Method names and behavior are as close as is reasonable to those in POSIX.

    Sample Usage. Here is a full version of a bounded buffer that implements the BoundedChannel interface, written in a style reminscent of that in POSIX programming books.

     class CVBuffer implements BoundedChannel {
       private final Mutex mutex;
       private final CondVar notFull;
       private final CondVar notEmpty;
       private int count = 0;
       private int takePtr = 0;     
       private int putPtr = 0;
       private final Object[] array;
     
       public CVBuffer(int capacity) { 
         array = new Object[capacity];
         mutex = new Mutex();
         notFull = new CondVar(mutex);
         notEmpty = new CondVar(mutex);
       }
     
       public int capacity() { return array.length; }
     
       public void put(Object x) throws InterruptedException {
         mutex.acquire();
         try {
           while (count == array.length) {
             notFull.await();
           }
           array[putPtr] = x;
           putPtr = (putPtr + 1) % array.length;
           ++count;
           notEmpty.signal();
         }
         finally {
           mutex.release();
         }
       }
     
       public Object take() throws InterruptedException {
         Object x = null;
         mutex.acquire();
         try {
           while (count == 0) {
             notEmpty.await();
           }
           x = array[takePtr];
           array[takePtr] = null;
           takePtr = (takePtr + 1) % array.length;
           --count;
           notFull.signal();
         }
         finally {
           mutex.release();
         }
         return x;
       }
     
       public boolean offer(Object x, long msecs) throws InterruptedException {
         mutex.acquire();
         try {
           if (count == array.length) {
             notFull.timedwait(msecs);
             if (count == array.length)
               return false;
           }
           array[putPtr] = x;
           putPtr = (putPtr + 1) % array.length;
           ++count;
           notEmpty.signal();
           return true;
         }
         finally {
           mutex.release();
         }
       }
       
       public Object poll(long msecs) throws InterruptedException {
         Object x = null;
         mutex.acquire();
         try {
           if (count == 0) {
             notEmpty.timedwait(msecs);
             if (count == 0)
               return null;
           }
           x = array[takePtr];
           array[takePtr] = null;
           takePtr = (takePtr + 1) % array.length;
           --count;
           notFull.signal();
         }
         finally {
           mutex.release();
         }
         return x;
       }
     }
    
     
    See Also:
    [ Introduction to this package. ]
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Sync mutex_
      The mutex
    • Constructor Summary

      Constructors 
      Constructor Description
      CondVar​(Sync mutex)
      Create a new CondVar that relies on the given mutual exclusion lock.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void await()
      Wait for notification.
      void broadcast()
      Notify all waiting threads
      void signal()
      Notify a waiting thread.
      boolean timedwait​(long msecs)
      Wait for at most msecs for notification.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • mutex_

        protected final Sync mutex_
        The mutex
    • Constructor Detail

      • CondVar

        public CondVar​(Sync mutex)
        Create a new CondVar that relies on the given mutual exclusion lock.
        Parameters:
        mutex - A non-reentrant mutual exclusion lock. Standard usage is to supply an instance of Mutex, but, for example, a Semaphore initialized to 1 also works. On the other hand, many other Sync implementations would not work here, so some care is required to supply a sensible synchronization object. In normal use, the mutex should be one that is used for all synchronization of the object using the CondVar. Generally, to prevent nested monitor lockouts, this object should not use any native Java synchronized blocks.
    • Method Detail

      • await

        public void await()
                   throws java.lang.InterruptedException
        Wait for notification. This operation at least momentarily releases the mutex. The mutex is always held upon return, even if interrupted.
        Throws:
        java.lang.InterruptedException - if the thread was interrupted before or during the wait. However, if the thread is interrupted after the wait but during mutex re-acquisition, the interruption is ignored, while still ensuring that the currentThread's interruption state stays true, so can be probed by callers.
      • timedwait

        public boolean timedwait​(long msecs)
                          throws java.lang.InterruptedException
        Wait for at most msecs for notification. This operation at least momentarily releases the mutex. The mutex is always held upon return, even if interrupted.
        Parameters:
        msecs - The time to wait. A value less than or equal to zero causes a momentarily release and re-acquire of the mutex, and always returns false.
        Returns:
        false if at least msecs have elapsed upon resumption; else true. A false return does NOT necessarily imply that the thread was not notified. For example, it might have been notified after the time elapsed but just before resuming.
        Throws:
        java.lang.InterruptedException - if the thread was interrupted before or during the wait.
      • signal

        public void signal()
        Notify a waiting thread. If one exists, a non-interrupted thread will return normally (i.e., not via InterruptedException) from await or timedwait.
      • broadcast

        public void broadcast()
        Notify all waiting threads