title: Execute coroutine (launch{}/Job) sequential order ( the order they called)
body: <p>How can I run multiple <code>coroutine</code> sequentially(like the order they were called ) inside a <code>coroutine</code>. I am creating multiple <code>coroutine</code> inside a <code>coroutine</code>, I want sequential execution as the order they are called.</p>
  <p>Here is my sample code.</p>
  <pre><code>fun main(){
      runBlocking {
          Launch().run()
      }
  }
  
  class Launch {
       fun run(){
          for (i in 1..10){
              println(&quot;Starting $i&quot;)
              GlobalScope.launch {
                  println(&quot;working id $i&quot;)
              }
          }
  
      }
  }
  </code></pre>
  <p>and output is</p>
  <pre><code>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
  </code></pre>
  <p>Inside the loop all <code>coroutine</code> 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?</p>
  <p><strong>Edit:</strong></p>
  <p><strong>Its not duplicate</strong></p>
  <p><a href="https://stackoverflow.com/questions/56480520/kotlin-coroutines-sequential-execution">This question answer</a> does not answer my question.</p>
  <blockquote>
  <p>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</p>
  </blockquote>
  <p>Tried with suggested approach</p>
  <pre><code>fun main(){
      runBlocking {
         // Launch().run()
          Launch().complex()
          delay(1000)
          println(&quot;Finish&quot;)
      }
  }
  
  class Launch {
      private val lock = Mutex()
  
      private fun func(name:String) {
          println(&quot;method $name&quot;)
          GlobalScope.launch {
              lock.withLock {
                  println(&quot;$name started&quot;)
                  println(&quot;$name completed&quot;)
              }
          }
      }
  
      fun complex() {
          func(&quot;1&quot;)
          func(&quot;2&quot;)
          func(&quot;3&quot;)
          func(&quot;4&quot;)
          func(&quot;5&quot;)
      }
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>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
  </code></pre>
  <p>Please check that after 1 completed, 3 is started but call order was <code>1,2,3,4,5</code>,  that means execution order  is not maintained as the called with <code>GlobalScope.launch</code></p>
  <p>I want to execute as the order they were called.</p>
  <p><strong>Expected output :</strong></p>
  <pre><code>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
  </code></pre>