Added in API level 1

ConditionVariable


public class ConditionVariable
extends Object

java.lang.Object
   ↳ android.os.ConditionVariable


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 as the object to wait on, so if you wait() or notify() on a ConditionVariable, the results are undefined.

Summary

Public constructors

ConditionVariable()

Create the ConditionVariable in the default closed state.

ConditionVariable(boolean state)

Create the ConditionVariable with the given state.

Public methods

void block()

Block the current thread until the condition is opened.

boolean block(long timeoutMs)

Block the current thread until the condition is opened or until timeoutMs milliseconds have passed.

void close()

Reset the condition to the closed state.

void open()

Open the condition, and release all threads that are blocked.

Inherited methods

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public constructors

ConditionVariable

Added in API level 1
public ConditionVariable ()

Create the ConditionVariable in the default closed state.

ConditionVariable

Added in API level 1
public ConditionVariable (boolean state)

Create the ConditionVariable with the given state.

Pass true for opened and false for closed.

Parameters
state boolean

Public methods

block

Added in API level 1
public void block ()

Block the current thread until the condition is opened.

If the condition is already opened, return immediately.

block

Added in API level 1
public boolean block (long timeoutMs)

Block the current thread until the condition is opened or until timeoutMs milliseconds have passed.

If the condition is already opened, return immediately.

Parameters
timeoutMs long: the maximum time to wait in milliseconds.

Returns
boolean true if the condition was opened, false if the call returns because of the timeout.

close

Added in API level 1
public void close ()

Reset the condition to the closed state.

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

open

Added in API level 1
public void open ()

Open the condition, and release all threads that are blocked.

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