# ConditionVariable

Artifact: [androidx.media3:media3-common](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/common/util/ConditionVariable.java+class:androidx.media3.common.util.ConditionVariable)

*** ** * ** ***

<br />

```
@UnstableApi
public class ConditionVariable
```

<br />

*** ** * ** ***

An interruptible condition variable. This class provides a number of benefits over `https://developer.android.com/reference/android/os/ConditionVariable.html`:

- Consistent use of (`https://developer.android.com/reference/androidx/media3/common/util/Clock#elapsedRealtime()` for timing `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#block(long)` timeout intervals. `https://developer.android.com/reference/android/os/ConditionVariable.html` used `https://developer.android.com/reference/java/lang/System.html#currentTimeMillis()` prior to Android 10, which is not a correct clock to use for interval timing because it's not guaranteed to be monotonic.
- Support for injecting a custom `https://developer.android.com/reference/androidx/media3/common/util/Clock`.
- The ability to query the variable's current state, by calling `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#isOpen()`.
- `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#open()` and `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#close()` return whether they changed the variable's state.

## Summary

| ### Public fields |
|---|---|
| `boolean` | `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#isOpen()` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#ConditionVariable()()` Creates a closed instance using `https://developer.android.com/reference/androidx/media3/common/util/Clock#DEFAULT()`. |
| `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#ConditionVariable(androidx.media3.common.util.Clock)(https://developer.android.com/reference/androidx/media3/common/util/Clock clock)` Creates an instance, which starts closed. |

| ### Public methods |
|---|---|
| `synchronized void` | `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#block()()` Blocks until the condition is opened. |
| `synchronized boolean` | `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#block(long)(long timeoutMs)` Blocks until the condition is opened or until `timeoutMs` have passed. |
| `synchronized void` | `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#blockUninterruptible()()` Blocks until the condition is open. |
| `synchronized boolean` | `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#blockUninterruptible(long)(long timeoutMs)` Blocks until the condition is open or until `timeoutMs` have passed. |
| `synchronized boolean` | `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#close()()` Closes the condition. |
| `synchronized boolean` | `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#isOpen()()` Returns whether the condition is opened. |
| `synchronized boolean` | `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#open()()` Opens the condition and releases all threads that are blocked. |

## Public fields

### isOpen

```
public boolean isOpen
```

## Public constructors

### ConditionVariable

```
public ConditionVariable()
```

Creates a closed instance using `https://developer.android.com/reference/androidx/media3/common/util/Clock#DEFAULT()`.

### ConditionVariable

```
public ConditionVariable(Clock clock)
```

Creates an instance, which starts closed.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/Clock clock` | The `https://developer.android.com/reference/androidx/media3/common/util/Clock` whose `https://developer.android.com/reference/androidx/media3/common/util/Clock#elapsedRealtime()` method is used to determine when `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#block(long)` should time out. |

## Public methods

### block

```
synchronized public void block()
```

Blocks until the condition is opened.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/InterruptedException.html` | If the thread is interrupted. |

### block

```
synchronized public boolean block(long timeoutMs)
```

Blocks until the condition is opened or until `timeoutMs` have passed.

| Parameters |
|---|---|
| `long timeoutMs` | The maximum time to wait in milliseconds. If `timeoutMs <= 0` then the call will return immediately without blocking. |

| Returns |
|---|---|
| `boolean` | True if the condition was opened, false if the call returns because of the timeout. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/InterruptedException.html` | If the thread is interrupted. |

### blockUninterruptible

```
synchronized public void blockUninterruptible()
```

Blocks until the condition is open.

Unlike `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#block()`, this method will continue to block if the calling thread is interrupted. If the calling thread was interrupted then its `https://developer.android.com/reference/java/lang/Thread.html#isInterrupted()` will be set when the method returns.

### blockUninterruptible

```
synchronized public boolean blockUninterruptible(long timeoutMs)
```

Blocks until the condition is open or until `timeoutMs` have passed.

Unlike `https://developer.android.com/reference/androidx/media3/common/util/ConditionVariable#block()`, this method will continue to block if the calling thread is interrupted. If the calling thread was interrupted then its `https://developer.android.com/reference/java/lang/Thread.html#isInterrupted()` will be set when the method returns.

| Parameters |
|---|---|
| `long timeoutMs` | The maximum time to wait in milliseconds. If `timeoutMs <= 0` then the call will return immediately without blocking. |

| Returns |
|---|---|
| `boolean` | True if the condition was opened, false if the call returns because of the timeout. |

### close

```
synchronized public boolean close()
```

Closes the condition.

| Returns |
|---|---|
| `boolean` | True if the condition variable was closed. False if it was already closed. |

### isOpen

```
synchronized public boolean isOpen()
```

Returns whether the condition is opened.

### open

```
synchronized public boolean open()
```

Opens the condition and releases all threads that are blocked.

| Returns |
|---|---|
| `boolean` | True if the condition variable was opened. False if it was already open. |