Coroutine

17 May 2024 . kotlin .

Challenges with Thread

  • Require a lot of resources
  • Race conditions and unpredictable behavior
fun main() {
   var count = 0
   for (i in 1..5) {
       Thread {
           count += 1
           println("Thread: $i count: $count")
       }.start()
   }
}

Output

Thread: 2 count: 2
Thread: 4 count: 4
Thread: 3 count: 3
Thread: 6 count: 6
Thread: 5 count: 5


Coroutines in Kotlin

Create and use threads for background tasks. A more flexible and easier way to manage concurrency.

import kotlinx.coroutines.*

fun main() {
    repeat(3) {
        GlobalScope.launch { //CoroutineScope
            println("Hi from ${Thread.currentThread()}") //Job
        }
    }
}