Solution: CounterContext
Here is a simple solution:
class CounterContext :
AbstractCoroutineContextElement(CounterContext){
private var counter = 0
fun next(): Int = counter++
companion object : CoroutineContext.Key<CounterContext>
}
Here is a thread-safe solution:
class CounterContext :
AbstractCoroutineContextElement(CounterContext){
private val counter = AtomicInteger(0)
fun next(): Int = counter.getAndIncrement()
companion object : CoroutineContext.Key<CounterContext>
}
import kotlinx.coroutines.*
import org.junit.Test
import java.util.concurrent.atomic.AtomicInteger
import kotlin.coroutines.*
import kotlin.test.assertEquals
class CounterContext :
AbstractCoroutineContextElement(CounterContext){
private var counter = 0
fun next(): Int = counter++
companion object : CoroutineContext.Key<CounterContext>
}
fun main(): Unit = runBlocking(CounterContext()) {
println(coroutineContext[CounterContext]?.next()) // 0
println(coroutineContext[CounterContext]?.next()) // 1
launch {
println(coroutineContext[CounterContext]?.next())// 2
println(coroutineContext[CounterContext]?.next())// 3
}
launch(CounterContext()) {
println(coroutineContext[CounterContext]?.next())// 0
println(coroutineContext[CounterContext]?.next())// 1
}
}
class CounterContextTests {
@Test
fun `should return next numbers in the same coroutine`() = runBlocking<Unit>(CounterContext()) {
assertEquals(0, coroutineContext[CounterContext]?.next())
assertEquals(1, coroutineContext[CounterContext]?.next())
assertEquals(2, coroutineContext[CounterContext]?.next())
assertEquals(3, coroutineContext[CounterContext]?.next())
assertEquals(4, coroutineContext[CounterContext]?.next())
}
@Test
fun `should have independent counter for each instance`() = runBlocking<Unit> {
launch(CounterContext()) {
assertEquals(0, coroutineContext[CounterContext]?.next())
assertEquals(1, coroutineContext[CounterContext]?.next())
assertEquals(2, coroutineContext[CounterContext]?.next())
}
launch(CounterContext()) {
assertEquals(0, coroutineContext[CounterContext]?.next())
assertEquals(1, coroutineContext[CounterContext]?.next())
assertEquals(2, coroutineContext[CounterContext]?.next())
}
}
@Test
fun `should propagate to the child`() = runBlocking<Unit>(CounterContext()) {
assertEquals(0, coroutineContext[CounterContext]?.next())
launch {
assertEquals(1, coroutineContext[CounterContext]?.next())
launch {
assertEquals(2, coroutineContext[CounterContext]?.next())
}
launch(CounterContext()) {
assertEquals(0, coroutineContext[CounterContext]?.next())
assertEquals(1, coroutineContext[CounterContext]?.next())
launch {
assertEquals(2, coroutineContext[CounterContext]?.next())
}
}
}
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.