Coroutine Flashcards
(48 cards)
What is a Coroutine?
A framework that helps manage concurrency in a performant and simple way.
What advantage does a Coroutine provide when writing code?
It allows you to write asynchronous code in a synchronous way to avoid callback hell and make the code easier to read.
How does Coroutine ensure safety in concurrency?
It follows structured concurrency, ensuring each coroutine is predictable and safe.
What is structured concurrency?
It’s a principle where concurrency is managed in a safe and predictable way to prevent memory leaks or unhandled exceptions.
How does structured concurrency ensure safe management of coroutines?
It ensures each coroutine is launched within a CoroutineScope that defines the lifetime of the coroutine.
What happens when the outer scope of structured concurrency is completed or canceled?
The outer scope can’t complete until all its children coroutines complete. When the outer scope is canceled, all its children coroutines are also canceled.
How are errors handled in structured concurrency?
Any errors are properly propagated to the outer scope.
What is a Dispatcher in Coroutine?
It determines which thread a coroutine runs on.
What is the default dispatcher for GlobalScope and regular coroutines?
The default dispatcher is Dispatchers.Default.
When should you use Dispatchers.Unconfined in Coroutine?
It’s used when there is no specific thread requirement.
How does Dispatchers.Unconfined behave when starting a coroutine?
Dispatchers.Unconfined starts the coroutine on the current thread but may switch to a different thread after resuming.
What are coroutineScope and supervisorScope in Kotlin Coroutines?
Both launch a new coroutine and ensure all coroutines within the scope complete before the scope returns.
What happens when a child coroutine fails in a coroutineScope?
If a child coroutine fails, the scope and all coroutines will be canceled. You should wrap the entire scope in a try-catch block to handle exceptions.
What happens when a child coroutine fails in a supervisorScope?
If a child coroutine fails, other child coroutines at the same level won’t be canceled, and the scope will continue until all coroutines complete. It’s best to add an individual try-catch block to each task to handle exceptions.
What is the suspend keyword used for in Kotlin?
It’s used to make a function suspensible and resumable without blocking the thread.
What does the compiler do when it encounters a suspend function?
The compiler converts the suspend function into a regular function with an additional parameter, Continuation, and changes its return type to Any.
Where must a suspend function be called?
A suspend function must be called within a coroutine or another suspend function.
What is Continuation in Kotlin Coroutines?
It stores the state of a coroutine and is used to resume the coroutine.
How does the compiler handle a suspend function in relation to Continuation?
The compiler converts the suspend function into a regular function with an additional parameter, Continuation, and changes its return type to Any.
What happens when a caller invokes a suspend function?
It passes its Continuation to the function. The suspend function checks whether the passed Continuation matches its expected type.
What occurs if the Continuation types don’t match in a suspend function?
It means this is the first time the suspend function is being called, so it creates its own Continuation and stores the caller’s Continuation.
How does Continuation help when a function encounters a suspend function?
The Continuation saves the current state of the function’s variables, marks the current position with a label, and returns a special marker COROUTINE_SUSPENDED to indicate the execution has been paused.
How is execution resumed in a coroutine using Continuation?
When execution needs to be resumed, the Continuation is used, and the function is called again. Since the caller’s Continuation matches the expected type, a new Continuation won’t be created. The function uses the label to identify the suspension point and the saved variables to continue execution from there.
What is withContext in Kotlin Coroutines?
It’s used to switch the coroutine’s context instead of launching a new coroutine.