Coroutine GlobalScope.async vs GlobalScope.launch

Hi,
I’m learning Kotlin Coroutines. Code below shows time measure between GlobalScope.async vs GlobalScope.launch. The problem is that result is same despite that first part is executed asynchronously.

 fun main(args: Array<String>) {
     runBlocking {
         println("aSync GlobalScope")
        var time = measureTimeMillis {
            val a1 = GlobalScope.async {
                delayOnSecond(1000)
                delayOnSecond(500)
            }
            val a2  = GlobalScope.async {
                delay(1000)
                delay(500)
            }
            a1.await()
            a2.await()
        }
        println("Total time: $time")
        println("sequential GlobalScope")

         time = measureTimeMillis {
             val a1 = GlobalScope.launch {
                delay(1000)
                delay(500)
            }
             val a2 = GlobalScope.launch {
                delay(1000)
                delay(500)
            }
            a1.join()
            a2.join()
        }
        println("Total time: $time")

    }

}

Shouldn’t be in second example 3000ms rather than 1500?

1 Like

They both execute asynchronously. The difference between async and launch is that async returns a value and launch doesn’t.

2 Likes