import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.withPermit val semaphore = Semaphore(5, 5) suspend fun main() { semaphore.acquire() //reduces available permits, suspends when none available semaphore.tryAcquire() //returns False if no permit available semaphore.release() //releases permit, possible after acquire semaphore.withPermit { } }