google-developer-training / android-basics-kotlin-mars-photos-app

Apache License 2.0
91 stars 193 forks source link

Android Basics: Coroutines no output #167

Closed DemisChan closed 8 months ago

DemisChan commented 1 year ago

name: Android Basics Unit 4 issue about: Report problems with the Android Basics Unit 4 codelabs title: 'Android Basics Unit 4 issue:' labels: '' assignees: ''


URL of codelab

In which task and step of the codelab can this issue be found?

import kotlinx.coroutines.*

fun main() {
    repeat(3) {
        GlobalScope.launch {
            println("Hi from ${Thread.currentThread()}")
        }
    }
}

Describe the problem There is no output that is mentioned below: Hi from Thread[DefaultDispatcher-worker-2@coroutine#2,5,main] Hi from Thread[DefaultDispatcher-worker-1@coroutine#1,5,main] Hi from Thread[DefaultDispatcher-worker-1@coroutine#3`,5,main]

If I add a Thread delay outside of globalScope lambda then it produces:

Hi from Thread[DefaultDispatcher-worker-1,5,main] Hi from Thread[DefaultDispatcher-worker-1,5,main] Hi from Thread[DefaultDispatcher-worker-1,5,main]

if I add delay(50) inside the lambda it doesnt do anything

Steps to reproduce?

  1. Go to...
  2. Click on...
  3. See error...

Versions Android Studio version: API version of the emulator:

Additional information Include screenshots if they would be useful in clarifying the problem.

DemisChan commented 1 year ago

Update: It seems on my laptop that when using GlobalScope it always runs on the same thread unless I use runBlocking with a join.