|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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; } }
Lock
Method Summary | |
---|---|
void |
await()
Wait for notification. |
void |
signal()
Notify a waiting thread. |
void |
signalAll()
Notify all waiting threads |
Method Detail |
---|
void await()
void signal()
void signalAll()
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |