1
votes

I created a simple program where I launched 3 coroutines using GlobalScope.launch which all print 100 lines of code of it's progress, coroutine 1 calls a suspend function when half of it's execution completed.

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

fun main(){
    println("Actual Program starts here : ${Thread.currentThread().name}")

    GlobalScope.launch {
        println("C1 starts here : ${Thread.currentThread().name}")
        for(i in 1..100){
            if(i == 50){
                mfunction()
            }
            println("C1 progress  = $i : ${Thread.currentThread().name}")
        }
        println("C1 Ends here : ${Thread.currentThread().name}")
    }

    GlobalScope.launch {
        println("C2 starts here : ${Thread.currentThread().name}")
        for(i in 1..100){
            println("C2 progress  = $i : ${Thread.currentThread().name}")
        }
        println("C2 Ends here : ${Thread.currentThread().name}")
    }

    GlobalScope.launch {
        println("C3 starts here : ${Thread.currentThread().name}")
        for(i in 1..100){
            println("C3 progress  = $i : ${Thread.currentThread().name}")
        }
        println("C3 Ends here : ${Thread.currentThread().name}")
    }

    for(i in 1..100){
        println("Actual Work progress  = $i : ${Thread.currentThread().name}")
    }
    println("Actual Program Ends here : ${Thread.currentThread().name}")
}

suspend fun mfunction(){
    for(i in 1..100){
        println("Suspend Work progress  = $i : ${Thread.currentThread().name}")
    }
}

As main thread is there by default. My Question is , will each coroutine create a new thread(for it's execution) to be run on or they all will run on a single background thread??

I executed this program, I get DefaultDispatcher-worker1 for Coroutine1, and DefaultDispatcher-worker2 for Coroutine2 and DefaultDispatcher-worker3 for Coroutine3 But sometimes i get DefaultDispatcher-worker1 for all coroutines.

I am not able to figure out the behaviour what is happening internally. Thanks in advance.

2

2 Answers

1
votes

Number of threads depends on which dispatcher was used and what your code does. Maximum number of threads that Dispatchers.Default (which you use here) can create is equal to the number of physical cores you have. If you would create e.g. 5 coroutines on 4-cores CPU then fifth coroutine would start after one of them would finish.

However, above is true only because your code is CPU-intensive and it doesn't at all suspend. If you would wait not with a busy loop, but through delay() then you could run even thousands of coroutines on a single thread.

Coroutines can also switch from one thread to another. You can start a coroutine in some thread and after a suspension point it could continue in another thread.

0
votes

will each coroutine create a new thread

Coroutines doesn't create threads. They use existing threads of some thread pool, which CoroutineDispatcher you run your couroutines on (Dispatchers.Default in your case) points to. Submitted tasks distribution on threads depends on internal implementation of respectful thread pool.