BSsync
Interface Condition


public interface Condition

This class implements condition variables similar to POSIX pthreads. If you restrict yourself to Locks and Conditions, 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 Lock mutex;
   private final Condition notFull;
   private final Condition 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 Lock();
     notFull = mutex.newCondition();
     notEmpty = mutex.newCondition();
   }
 
   public int capacity() { return array.length; }
 
   public void put(Object x) {
       mutex.lock();
       while (count == array.length) {
         notFull.await();
       }
       array[putPtr] = x;
       putPtr = (putPtr + 1) % array.length;
       ++count;
       notEmpty.signal();
       mutex.unlock();
   }
 
   public Object take() {
       Object x = null;
       mutex.lock();
       while (count == 0) {
         notEmpty.await();
       }
       x = array[takePtr];
       array[takePtr] = null;
       takePtr = (takePtr + 1) % array.length;
       --count;
       notFull.signal();
       mutex.unlock();
       return x;
   }
 }

 

See Also:
Lock

Method Summary
 void await()
          Wait for notification.
 void signal()
          Notify a waiting thread.
 void signalAll()
          Notify all waiting threads
 

Method Detail

await

void await()
Wait for notification. This operation at least momentarily releases the mutex. The mutex is always acquired back before the method returns.


signal

void signal()
Notify a waiting thread. If one exists, a thread will return from await.


signalAll

void signalAll()
Notify all waiting threads