Sobes.tech
Back to tasks
Junior — Senior
104

Что произойдёт с переменной при множественном запуске корутин?

Get help with live coding in real time with Sobes Copilot
Task condition

Определите, какой результат будет напечатан в консоль после выполнения следующего кода:

val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
var i = 0

fun main() = runBlocking {
    repeat(100000) {
        increase()
    }

    println(i)
}

fun increase() = scope.launch {
    synchronized(this) {
        i++
    }
}