Why can you run a Kotlin coroutine on the main thread

0 votes

I don't understand how this piece of code can function correctly:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    launch(Dispatchers.Main) {
        log("A")
    }

    log("B")
}

This should output B first, followed by A.

Does this work give that the main thread is already under coroutine control? Maybe code is magically injected into the main thread by the coroutines API.

Nov 18, 2022 in Kotlin by Tejashwini
• 3,820 points
758 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
In Android (and other UI frameworks as well), the UI/main thread operates an "event loop." It does this by waiting for tasks to be planned for it, having a queue of them, and carrying them out in order. For instance, an internal onClick action is planned to be executed on the main thread when you click a button. However, the user is also permitted to manually schedule their jobs using methods like runOnUiThread() or getMainLooper ().

Dispatchers.

The main thread can be scheduled with just one more method, the main. Coroutines do not automatically assume complete control over the main thread or magically introduce new content. The cooperative main thread enables task and coroutine scheduling.

Additionally, you questioned in the comments how it was feasible for both log statements to be executed concurrently on the same thread. They do not operate simultaneously. Only log("A") is added to the queue by onCreate() and scheduled to run later. When onCreate() completes, log("B") is then called, and only then can the main thread begin executing the log("A") block. In other words, although not in top-to-bottom order, this is sequential.
answered Nov 24, 2022 by Ashwini
• 5,430 points

edited Mar 5

Related Questions In Kotlin

0 votes
0 answers

What's the difference between !! and ? in Kotlin?

Kotlin is new to me. What makes ...READ MORE

Nov 18, 2022 in Kotlin by Tejashwini
• 3,820 points
502 views
0 votes
0 answers

Accessibility implementation for Google Maps Marker in Android Kotlin

have to announce some customised content for ...READ MORE

Jun 22, 2022 in Kotlin by Harshitha
• 160 points
657 views
0 votes
0 answers

What is .indices meaning in kotlin?

I want to know actually how .indices ...READ MORE

Jun 22, 2022 in Kotlin by Harshitha
• 160 points
641 views
0 votes
0 answers

Which standard library to use in Kotlin?

When using the JVM with Kotlin, it ...READ MORE

Nov 18, 2022 in Kotlin by Tejashwini
• 3,820 points
542 views
0 votes
0 answers

What does the suspend function mean in a Kotlin Coroutine?

I'm reading Kotlin Coroutine and know that ...READ MORE

May 26, 2022 in Others by Harshitha
• 120 points
499 views
0 votes
0 answers

What's the different between native-mt and normal kotlin coroutine lib?

When using a coroutine, we have the ...READ MORE

Nov 18, 2022 in Kotlin by Tejashwini
• 3,820 points
688 views
0 votes
0 answers

Call Kotlin suspend function in Java class

Assume we have the following function: suspend fun doSomething(): ...READ MORE

Nov 18, 2022 in Kotlin by Tejashwini
• 3,820 points
2,017 views
+1 vote
4 answers

Android - java or kotlin is better

If you are a newbie Android developer, ...READ MORE

answered Jan 22, 2020 in Career Counselling by Nayan
• 160 points
1,713 views
0 votes
0 answers

How can I monitor the thread count of a process on linux?

I would like to monitor the number ...READ MORE

Apr 21, 2022 in Linux Administration by Edureka
• 13,690 points
529 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP