FileDocCategorySizeDatePackage
ConditionVariable.javaAPI DocAndroid 1.5 API3924Wed May 06 22:41:56 BST 2009android.os

ConditionVariable

public class ConditionVariable extends Object
Class that implements the condition variable locking paradigm.

This differs from the built-in java.lang.Object wait() and notify() in that this class contains the condition to wait on itself. That means open(), close() and block() are sticky. If open() is called before block(), block() will not block, and instead return immediately.

This class uses itself is at the object to wait on, so if you wait() or notify() on a ConditionVariable, the results are undefined.

Fields Summary
private volatile boolean
mCondition
Constructors Summary
public ConditionVariable()
Create the ConditionVariable in the default closed state.

        mCondition = false;
    
public ConditionVariable(boolean state)
Create the ConditionVariable with the given state.

Pass true for opened and false for closed.

        mCondition = state;
    
Methods Summary
public voidblock()
Block the current thread until the condition is opened.

If the condition is already opened, return immediately.

        synchronized (this) {
            while (!mCondition) {
                try {
                    this.wait();
                }
                catch (InterruptedException e) {
                }
            }
        }
    
public booleanblock(long timeout)
Block the current thread until the condition is opened or until timeout milliseconds have passed.

If the condition is already opened, return immediately.

param
timeout the minimum time to wait in milliseconds.
return
true if the condition was opened, false if the call returns because of the timeout.

        // Object.wait(0) means wait forever, to mimic this, we just
        // call the other block() method in that case.  It simplifies
        // this code for the common case.
        if (timeout != 0) {
            synchronized (this) {
                long now = System.currentTimeMillis();
                long end = now + timeout;
                while (!mCondition && now < end) {
                    try {
                        this.wait(end-now);
                    }
                    catch (InterruptedException e) {
                    }
                    now = System.currentTimeMillis();
                }
                return mCondition;
            }
        } else {
            this.block();
            return true;
        }
    
public voidclose()
Reset the condition to the closed state.

Any threads that call block() will block until someone calls open.

        synchronized (this) {
            mCondition = false;
        }
    
public voidopen()
Open the condition, and release all threads that are blocked.

Any threads that later approach block() will not block unless close() is called.

        synchronized (this) {
            boolean old = mCondition;
            mCondition = true;
            if (!old) {
                this.notifyAll();
            }
        }