CoRoutine Async is not running parallely

I have the following code that I am trying out to run coroutines parallel. However, the code does not run parallel. It takes 30secs for all the stock prices to return instead of 10 secs. However, If I use GlobalScope.launch, it does work correctly. I gather from the documentation that the We should avoid using GlobalScope and use coroutine scope. Can you help in understanding why this is not running in parallel?

import kotlinx.coroutines.*

suspend fun getStockPrice(company: String) : Int{
    println("Fetching Stock Price")
    Thread.sleep(10000)
    return 100
}



fun CoroutineScope.launchCoRoutines() {
    val companies = listOf<String>("Google", "Amazon", "Microsoft")

    launch {

        var startTime = System.currentTimeMillis()
        val sharePrice = mutableListOf<Deferred<Int>>()
        for (company in companies) {
            sharePrice += async {
                getStockPrice(company).toInt()
            }
        }

        for (share in sharePrice) {
            println(share.await())
        }
        var endTime = System.currentTimeMillis()

        println(endTime - startTime)

    }


}

 fun main()  {
     runBlocking{
         launchCoRoutines()
     }
     println("Request Sent")
    Thread.sleep(55000)
}

Upvotes: 1

Views: 1454

Answers (1)

benjiii
benjiii

Reputation: 553

The answer can be found here:

Kotlin: coroutineScope is slower than GlobalScope

as said:

coroutineScope does not define its own dispatcher so you inherit it from the caller, in this case the one created by runBlocking. It uses the single thread it is called on.

EDIT: you could use this function

fun launchCoRoutines() {
    val companies = listOf<String>("Google", "Amazon", "Microsoft")

    launch(Dispatchers.Default) { // will get dispatched to DefaultDispatcher 

        var startTime = System.currentTimeMillis()
        val sharePrice = mutableListOf<Deferred<Int>>()
        for (company in companies) {
            sharePrice += async {
                getStockPrice(company).toInt()
            }
        }

        for (share in sharePrice) {
            println(share.await())
        }
        var endTime = System.currentTimeMillis()

        println(endTime - startTime)
    }
}

EDIT 2: See @Animesh Sahu comment, it is the best answer

Upvotes: 2

Related Questions