Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
Is there a difference between using GlobalScope.launch and CoroutineScope().launch to launch a coroutine?

There are different ways of launching a coroutine in Kotlin. I found a couple of examples where GlobalScope and CoroutineScope are used. But the latter one is being created directly when launching a coroutine:

  1. Using GlobalScope:

    fun loadConfiguration() {
        GlobalScope.launch(Dispatchers.Main) {
           val config = fetchConfigFromServer() // network request
           updateConfiguration(config)
        }
    }
    
  2. Using CoroutineScope instances, created directly when launching a coroutine:

    fun loadConfiguration() {
        CoroutineScope(Dispatchers.Main).launch {
            val config = fetchConfigFromServer() // network request
            updateConfiguration(config)
        }
    }
    

In this case is there a difference between these two approaches?

Doesn't the second case violate the principle of structured concurrency?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Doesn't the second case violate the principle of structured concurrency?

Actually both cases violate it equally, because they have pretty much the exact same semantics. All the warnings against using GlobalScope.launch that you may have encountered, apply to CoroutineScope().launch just the same. A slight difference is that the latter is entirely pointless, whereas the former at least has some legitimate use cases.

Writing CoroutineScope().launch instead of GlobalScope.launch obscures the intent and consequences, as well as creates a needless object every time.

I think the CoroutineScope().launch idiom came into being solely because it circumvented the warnings you get for GlobalScope.launch.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda