Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/os/ConditionVariable#pubctors) \| [Methods](https://developer.android.com/reference/android/os/ConditionVariable#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/os/ConditionVariable#inhmethods)

# ConditionVariable

*** ** * ** ***

` public class ConditionVariable `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.os.ConditionVariable |

<br />

*** ** * ** ***

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 ||
|---|---|
| ` https://developer.android.com/reference/android/os/ConditionVariable#ConditionVariable()() ` Create the ConditionVariable in the default closed state. |
| ` https://developer.android.com/reference/android/os/ConditionVariable#ConditionVariable(boolean)(boolean state) ` Create the ConditionVariable with the given state. |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/os/ConditionVariable#block()() ` Block the current thread until the condition is opened. |
| ` boolean` | ` https://developer.android.com/reference/android/os/ConditionVariable#block(long)(long timeoutMs) ` Block the current thread until the condition is opened or until timeoutMs milliseconds have passed. |
| ` void` | ` https://developer.android.com/reference/android/os/ConditionVariable#close()() ` Reset the condition to the closed state. |
| ` void` | ` https://developer.android.com/reference/android/os/ConditionVariable#open()() ` Open the condition, and release all threads that are blocked. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(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` ` https://developer.android.com/reference/java/lang/Object#wait(long)(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` ` https://developer.android.com/reference/java/lang/Object#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](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ConditionVariable ()
```

Create the ConditionVariable in the default closed state.

<br />

### ConditionVariable

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ConditionVariable (boolean state)
```

Create the ConditionVariable with the given state.

Pass true for opened and false for closed.

<br />

| Parameters ||
|---|---|
| `state` | `boolean` <br /> |

## Public methods

### block

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void block ()
```

Block the current thread until the condition is opened.

If the condition is already opened, return immediately.

<br />

### block

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `timeoutMs` | `long`: the maximum time to wait in milliseconds. <br /> |

| Returns ||
|---|---|
| `boolean` | true if the condition was opened, false if the call returns because of the timeout. <br /> |

### close

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void close ()
```

Reset the condition to the closed state.

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

<br />

### open

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />