3

How can I run multiple coroutine sequentially(like the order they were called ) inside a coroutine. I am creating multiple coroutine inside a coroutine, I want sequential execution as the order they are called.

Here is my sample code.

fun main(){
    runBlocking {
        Launch().run()
    }
}

class Launch {
     fun run(){
        for (i in 1..10){
            println("Starting $i")
            GlobalScope.launch {
                println("working id $i")
            }
        }

    }
}

and output is

Starting 1
Starting 2
Starting 3
Starting 4
Starting 5
Starting 6
Starting 7
Starting 8
working id 5
working id 4
working id 3
Starting 9
working id 1
Starting 10
working id 2
working id 8
working id 7
working id 6
working id 9
working id 10

Inside the loop all coroutine are executing asynchronously, I know it's natural behavior of coroutine. But I want to execute all the coroutine sequentially how can I do that?

Edit:

Its not duplicate

This question answer does not answer my question.

That answer works for one block execution at a time, but don't maintain order. I need to maintain execution order as like they called

Tried with suggested approach

fun main(){
    runBlocking {
       // Launch().run()
        Launch().complex()
        delay(1000)
        println("Finish")
    }
}

class Launch {
    private val lock = Mutex()

    private fun func(name:String) {
        println("method $name")
        GlobalScope.launch {
            lock.withLock {
                println("$name started")
                println("$name completed")
            }
        }
    }

    fun complex() {
        func("1")
        func("2")
        func("3")
        func("4")
        func("5")
    }
}

Output:

method 1
method 2
method 3
method 4
method 5
1 started
1 completed
3 started
3 completed
4 started
4 completed
2 started
2 completed
5 started
5 completed
Finish

Please check that after 1 completed, 3 is started but call order was 1,2,3,4,5, that means execution order is not maintained as the called with GlobalScope.launch

I want to execute as the order they were called.

Expected output :

method 1
method 2
method 3
method 4
method 5
1 started
1 completed
2 started
2 completed
3 started
3 completed
4 started
4 completed
5 started
5 completed
Finish
Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
  • 1
    Have a look [here](https://stackoverflow.com/questions/72318010/how-to-make-coroutines-launch-next-job-only-when-first-is-finished/72319023#72319023) or [here](https://stackoverflow.com/questions/56480520/kotlin-coroutines-sequential-execution/63494023#63494023). You can use a Channel to do sequential coroutine execution like a queue. – Tyler V May 27 '22 at 04:34
  • Thanks. I have checked those, hoping is there any easy other approach available. – Abu Yousuf May 27 '22 at 04:42
  • The answers to those questions cover pretty much all the available options - they really aren't that complicated. What's the problem with using a channel? That's basically what it's for, to act like a queue. Check its [docs](https://kotlinlang.org/docs/channels.html) for more info. – Tyler V May 27 '22 at 04:45
  • Tried with `Mutex` not working. changed the `run` method with `lock.withLock { .. for loop here.. }` but not working . Executing asynchronously – Abu Yousuf May 27 '22 at 04:51
  • Does this answer your question? [Kotlin Coroutines sequential execution](https://stackoverflow.com/questions/56480520/kotlin-coroutines-sequential-execution) – sidgate May 27 '22 at 05:18
  • @sidgate no, tried that solution – Abu Yousuf May 27 '22 at 05:22
  • I have added explanation why this question is not duplicate. – Abu Yousuf May 27 '22 at 05:36
  • 1
    Using `Channel` with `launch(start = CoroutineStart.LAZY)` seems to be the key. – Ricky Mo May 27 '22 at 05:56
  • You can also use `Flow`s to solve the call order problem, please see https://stackoverflow.com/a/72332468/1731626. – Sergio May 27 '22 at 07:09
  • Using `Mutex` is not guaranteed the order of execution. – Sergio May 27 '22 at 07:11
  • Like we have said here at least three times now, use a channel if you need to guarantee order. Scroll down to the last example in the answer [here](https://stackoverflow.com/questions/56480520/kotlin-coroutines-sequential-execution/63494023#63494023), the one using a channel. – Tyler V May 27 '22 at 12:01
  • ok i will check – Abu Yousuf May 27 '22 at 12:18

0 Answers0