singleThreadContext

suspend fun ResourceScope.singleThreadContext(name: String): ExecutorCoroutineDispatcher(source)

Creates a single threaded CoroutineContext as a Resource. Upon release an orderly shutdown of the ExecutorService takes place in which previously submitted tasks are executed, but no new tasks will be accepted.

import arrow.fx.coroutines.resourceScope
import arrow.fx.coroutines.singleThreadContext
import kotlinx.coroutines.withContext
import kotlinx.coroutines.ExecutorCoroutineDispatcher

suspend fun main(): Unit = resourceScope {
val single: ExecutorCoroutineDispatcher = singleThreadContext("single")
withContext(single) {
println("I am running on ${Thread.currentThread().name}")
}
}
I am running on single

fun singleThreadContext(name: String): Resource<ExecutorCoroutineDispatcher>(source)