arrow-fx-coroutines / arrow.fx.coroutines / Semaphore / acquireN

acquireN

abstract suspend fun acquireN(n: Long): Unit

Acquires n permits, suspends until the required permits are available. When it gets cancelled while suspending, it will release its already acquired permits.

import arrow.fx.coroutines.*

suspend fun main(): Unit {
  //sampleStart
  val semaphore = Semaphore(5)

  timeOutOrNull(2.seconds) {
    semaphore.acquireN(10) // count: -5
  }.also { println("I timed out with: $it") }

  semaphore.acquireN(5)
  val available = semaphore.available()
  //sampleEnd
  println(available)
}

Parameters

n - number of permits to acquire; must be greater or equal than zero.

Do you like Arrow?

Arrow Org
<