ObjectPool

interface ObjectPool<T : Any> : Closeable

Properties

capacity

abstract val capacity: Int

Pool capacity

Functions

borrow

abstract fun borrow(): T

borrow an instance. Pool can recycle an old instance or create a new one

close

open fun close(): Unit

Does pool dispose

dispose

abstract fun dispose(): Unit

Dispose the whole pool. None of borrowed objects could be used after the pool gets disposed otherwise it can result in undefined behaviour

recycle

abstract fun recycle(instance: T): Unit

Recycle an instance. Should be recycled what was borrowed before otherwise could fail

Extension Functions

useBorrowed

fun <T : Any, R> ObjectPool<T>.useBorrowed(
    block: (T) -> R
): R

Borrows and instance of T from the pool, invokes block with it and finally recycles it

useInstance

fun <T : Any, R> ObjectPool<T>.useInstance(
    block: (T) -> R
): R

Borrows and instance of T from the pool, invokes block with it and finally recycles it

Inheritors

DefaultPool

abstract expect class DefaultPool<T : Any> : ObjectPool<T>

Default object pool implementation.

NoPoolImpl

abstract class NoPoolImpl<T : Any> : ObjectPool<T>

A pool implementation of zero capacity that always creates new instances

SingleInstancePool

abstract class SingleInstancePool<T : Any> : ObjectPool<T>

A pool that produces at most one instance