Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
Will GlobalScope.launch run instantly?

for example this piece of code:

repeat(1000) { index ->
    GlobalScope.launch(MAIN) {
        println(index.toString())
    }
}

Is it possible to print not orderly like 0 1 3 2 4... ?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

First, keep in mind that you should very probably NOT use GlobalScope in this case. It's only meant for launching coroutines that should keep running for the entire life of the application.

Also, in essence, when launching asynchronous blocks of code like this, it should not really matter to you when exactly they run. The contract is that they run concurrently with the rest of the code (at least until you join() the returned Job, if you do). If you wanted to launch multiple concurrent coroutines but get some results in-order, then you still wouldn't have to care about the order of execution of the coroutines, but just about getting the result of each one in the right place. For this you could use map+async+awaitAll instead of launch.

Thirdly, if you have suspension points (calls to suspend functions) in your coroutines, their execution will be interlaced, so the notion of running before or after other coroutines will get fuzzy.

With that in mind, let's answer the question. The execution order (and parallelism) will depend on the dispatcher you provide in the coroutine context.

  • If it is Android's Dispatchers.Main.immediate or Dispatchers.Unconfined, it will execute these coroutines immediately - meaning in-order in this case, since you have no suspension points.

  • If it is Android's Dispatchers.Main, which is single-threaded, coroutines will be "queued" on the main thread, but likely still run in-order eventually in this case. However you shouldn't rely on this as this would be an implementation detail, not an API guarantee.

  • With multi-threaded dispatchers like Dispatchers.Default or Dispatchers.IO, the coroutines will be run in parallel with no order guarantees.

  • GlobalScope on its own doesn't have a dispatcher, so if you launch coroutines without custom context like GlobalScope.launch { ... }, they will run on Dispatchers.Default which is multi-threaded and could run your coroutines in parallel with no order guarantees.

  • GlobalScope.launch(MAIN) { ... } provides a custom coroutine context called MAIN. I don't know what is defined in this constant for you. If this context contains a dispatcher, then the dispatcher will decide as mentioned above. If not, Dispatchers.Default will be used by default by launch.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda