@MainThread
@UnstableApi
public final class PlayerPool<T extends Player>


A pool of Player instances.

All operations on this class must be called from the main thread.

Parameters
<T extends Player>

The type of the Player instances in the pool.

Summary

Public constructors

<T extends Player> PlayerPool(
    int poolCapacity,
    @NonNull Function0<@NonNull T> playerFactory
)

Public methods

final @NonNull T

Suspends until a player is available in the pool.

final void

Executes the given action only on players that are currently acquired from the pool.

final void
@MainThread
executeForAll(@NonNull Function1<@NonNull T, Unit> action)

Executes the given action on all players managed by the pool, including both acquired players and idle players waiting in the pool.

final void

Releases all players in the pool and prevents further acquisition.

final void
yield(@NonNull T player)

Returns a player to the pool.

Public constructors

PlayerPool

public <T extends Player> PlayerPool(
    int poolCapacity,
    @NonNull Function0<@NonNull T> playerFactory
)
Parameters
<T extends Player>

The type of the Player instances in the pool.

int poolCapacity

The maximum number of players to hold in the pool. Must be positive.

@NonNull Function0<@NonNull T> playerFactory

A lambda responsible for instantiating and configuring new players. The created players must be configured to be accessed from the main thread.

Public methods

acquire

public final @NonNullacquire()

Suspends until a player is available in the pool.

executeForAcquired

@MainThread
public final void executeForAcquired(@NonNull Function1<@NonNull T, Unit> action)

Executes the given action only on players that are currently acquired from the pool.

This is useful for broadcasting state changes (like muting or updating playback speed) to players that are currently active, without affecting idle players in the pool.

Parameters
@NonNull Function1<@NonNull T, Unit> action

The action to perform on each acquired player.

executeForAll

@MainThread
public final void executeForAll(@NonNull Function1<@NonNull T, Unit> action)

Executes the given action on all players managed by the pool, including both acquired players and idle players waiting in the pool.

Parameters
@NonNull Function1<@NonNull T, Unit> action

The action to perform on each player.

release

public final void release()

Releases all players in the pool and prevents further acquisition.

yield

public final void yield(@NonNull T player)

Returns a player to the pool.

Parameters
@NonNull T player

The Player instance to return to the pool.