# BackgroundThreadStateHandler

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/BackgroundThreadStateHandler.java+class:androidx.media3.common.util.BackgroundThreadStateHandler)

*** ** * ** ***


```
@UnstableApi
public final class BackgroundThreadStateHandler<T extends Object>
```

<br />

*** ** * ** ***

Helper class to handle state updates on a background thread while maintaining a placeholder state on the foreground thread.

| Parameters |
|---|---|
| `<T extends https://developer.android.com/reference/java/lang/Object.html>` | An immutable object representing the entire state. Must implement `https://developer.android.com/reference/java/lang/Object.html#equals(java.lang.Object)`. |

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/media3/common/util/BackgroundThreadStateHandler.StateChangeListener<T extends https://developer.android.com/reference/java/lang/Object.html>` An interface to handle changes to the state on the foreground thread. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/common/util/BackgroundThreadStateHandler#BackgroundThreadStateHandler(T,android.os.Looper,android.os.Looper,androidx.media3.common.util.Clock,androidx.media3.common.util.BackgroundThreadStateHandler.StateChangeListener<T>)( T initialState, https://developer.android.com/reference/android/os/Looper.html backgroundLooper, https://developer.android.com/reference/android/os/Looper.html foregroundLooper, https://developer.android.com/reference/androidx/media3/common/util/Clock clock, https://developer.android.com/reference/androidx/media3/common/util/BackgroundThreadStateHandler.StateChangeListener<T> onStateChanged )` Creates the helper for background thread state updates. |

| ### Public methods |
|---|---|
| `T` | `https://developer.android.com/reference/androidx/media3/common/util/BackgroundThreadStateHandler#get()()` Returns the current state. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/BackgroundThreadStateHandler#runInBackground(java.lang.Runnable)(https://developer.android.com/reference/java/lang/Runnable.html runnable)` Runs the provided `https://developer.android.com/reference/java/lang/Runnable.html` on the background thread. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/BackgroundThreadStateHandler#setStateInBackground(T)(T newState)` Updates the background state directly, independent to any operation started from the foreground thread. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/BackgroundThreadStateHandler#updateStateAsync(java.util.function.Function<T,T>,java.util.function.Function<T,T>)( https://developer.android.com/reference/java/util/function/Function.html<T, T> placeholderState, https://developer.android.com/reference/java/util/function/Function.html<T, T> backgroundStateUpdate )` Starts an asynchronous state update. |

## Public constructors

### BackgroundThreadStateHandler

```
public BackgroundThreadStateHandler(
    T initialState,
    Looper backgroundLooper,
    Looper foregroundLooper,
    Clock clock,
    BackgroundThreadStateHandler.StateChangeListener<T> onStateChanged
)
```

Creates the helper for background thread state updates.

This constructor may be called on any thread.

| Parameters |
|---|---|
| `T initialState` | The initial state value. |
| `https://developer.android.com/reference/android/os/Looper.html backgroundLooper` | The `https://developer.android.com/reference/android/os/Looper.html` to run background operations on. |
| `https://developer.android.com/reference/android/os/Looper.html foregroundLooper` | The `https://developer.android.com/reference/android/os/Looper.html` to run foreground operations on. |
| `https://developer.android.com/reference/androidx/media3/common/util/Clock clock` | The `https://developer.android.com/reference/androidx/media3/common/util/Clock` to control the handler messages. |
| `https://developer.android.com/reference/androidx/media3/common/util/BackgroundThreadStateHandler.StateChangeListener<T> onStateChanged` | The `https://developer.android.com/reference/androidx/media3/common/util/BackgroundThreadStateHandler.StateChangeListener` to listen to state changes. |

## Public methods

### get

```
public T get()
```

Returns the current state.

Can be called on either the foreground or background thread, returning the respective current state of this thread.

### runInBackground

```
public void runInBackground(Runnable runnable)
```

Runs the provided `https://developer.android.com/reference/java/lang/Runnable.html` on the background thread.

Can be called from any thread.

Note: This method is useful to update the state on the background using `https://developer.android.com/reference/androidx/media3/common/util/BackgroundThreadStateHandler#setStateInBackground(T)` for events arriving from external sources. Use `https://developer.android.com/reference/androidx/media3/common/util/BackgroundThreadStateHandler#updateStateAsync(java.util.function.Function<T,T>,java.util.function.Function<T,T>)` if the intention is update the state in response the a foreground thread method call.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/Runnable.html runnable` | The `https://developer.android.com/reference/java/lang/Runnable.html` to be called on the background thread. |

### setStateInBackground

```
public void setStateInBackground(T newState)
```

Updates the background state directly, independent to any operation started from the foreground thread.

Must only be called on the background thread.

| Parameters |
|---|---|
| `T newState` | The new state. |

### updateStateAsync

```
public void updateStateAsync(
    Function<T, T> placeholderState,
    Function<T, T> backgroundStateUpdate
)
```

Starts an asynchronous state update.

Must only be called on the foreground thread.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/util/function/Function.html<T, T> placeholderState` | A function to create a placeholder state from the current state while the operation is pending. Will be called on the foreground thread. |
| `https://developer.android.com/reference/java/util/function/Function.html<T, T> backgroundStateUpdate` | A function to handle the background state update, taking in the current background state and returning the updated state. Will be called on the background thread. |