Coroutines, suspend functions, dispatchers, structured concurrency, channels, Flow and StateFlow.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- What is a suspend function in Kotlin?
Show answer
A function that can be paused and resumed without blocking the thread, enabling non-blocking concurrent operations.
- What does the suspend keyword do?
Show answer
Marks a function as suspendable, so it can call other suspend functions and be invoked only from coroutines or another suspend function.
- What does the launch builder return?
Show answer
A Job object representing the launched coroutine, without blocking.
- What does the async builder return?
Show answer
A Deferred object that represents a future result that can be awaited.
- What does runBlocking do?
Show answer
Blocks the current thread until the coroutine completes; used mainly in tests or main functions.
- What is structured concurrency?
Show answer
A paradigm where each coroutine is launched in a scope and all children must complete before the scope exits, preventing leaked coroutines.
- What is a CoroutineScope?
Show answer
An interface defining the lifetime and hierarchy of coroutines within it.
- What does Job represent?
Show answer
The lifecycle of a coroutine, with states like New, Active, Completing, and Completed.
- What is Dispatcher.Main?
Show answer
A coroutine dispatcher that runs code on the main thread, used for UI updates.
- What is Dispatcher.Default?
Show answer
A dispatcher for CPU-intensive work, using a thread pool sized to the number of CPU cores.
- What is Dispatcher.IO?
Show answer
A dispatcher optimized for blocking IO operations like network or file access.
- What is Dispatcher.Unconfined?
Show answer
A dispatcher that does not enforce thread affinity; generally not recommended for production.
- What does withContext do?
Show answer
Switches the dispatcher for a block of code and resumes on the original dispatcher after completion.
- Why is GlobalScope discouraged?
Show answer
It breaks structured concurrency because coroutines are not tied to a parent scope and can leak.
- What is the difference between Job and SupervisorJob?
Show answer
SupervisorJob does not propagate child failures to the parent, while Job does.