//arrow-fx-coroutines/arrow.fx.coroutines/singleThreadContext

singleThreadContext

jvm fun Resource.Companion.singleThreadContext(name: String): Resource<CoroutineContext>

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.*
import kotlinx.coroutines.withContext

val singleCtx = Resource.singleThreadContext("single")

suspend fun main(): Unit =
  singleCtx.use { ctx ->
    withContext(ctx) {
      println("I am running on ${Thread.currentThread().name}")
    }
  }

Do you like Arrow?

Arrow Org
<