Coroutine Flashcards

1
Q

What is a Coroutine?

A

A framework that helps manage concurrency in a performant and simple way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What advantage does a Coroutine provide when writing code?

A

It allows you to write asynchronous code in a synchronous way to avoid callback hell and make the code easier to read.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does Coroutine ensure safety in concurrency?

A

It follows structured concurrency, ensuring each coroutine is predictable and safe.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is structured concurrency?

A

It’s a principle where concurrency is managed in a safe and predictable way to prevent memory leaks or unhandled exceptions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does structured concurrency ensure safe management of coroutines?

A

It ensures each coroutine is launched within a CoroutineScope that defines the lifetime of the coroutine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens when the outer scope of structured concurrency is completed or canceled?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How are errors handled in structured concurrency?

A

Any errors are properly propagated to the outer scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Dispatcher in Coroutine?

A

It determines which thread a coroutine runs on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the default dispatcher for GlobalScope and regular coroutines?

A

The default dispatcher is Dispatchers.Default.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When should you use Dispatchers.Unconfined in Coroutine?

A

It’s used when there is no specific thread requirement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does Dispatchers.Unconfined behave when starting a coroutine?

A

Dispatchers.Unconfined starts the coroutine on the current thread but may switch to a different thread after resuming.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are coroutineScope and supervisorScope in Kotlin Coroutines?

A

Both launch a new coroutine and ensure all coroutines within the scope complete before the scope returns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens when a child coroutine fails in a coroutineScope?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens when a child coroutine fails in a supervisorScope?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the suspend keyword used for in Kotlin?

A

It’s used to make a function suspensible and resumable without blocking the thread.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the compiler do when it encounters a suspend function?

A

The compiler converts the suspend function into a regular function with an additional parameter, Continuation, and changes its return type to Any.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Where must a suspend function be called?

A

A suspend function must be called within a coroutine or another suspend function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is Continuation in Kotlin Coroutines?

A

It stores the state of a coroutine and is used to resume the coroutine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How does the compiler handle a suspend function in relation to Continuation?

A

The compiler converts the suspend function into a regular function with an additional parameter, Continuation, and changes its return type to Any.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What happens when a caller invokes a suspend function?

A

It passes its Continuation to the function. The suspend function checks whether the passed Continuation matches its expected type.

21
Q

What occurs if the Continuation types don’t match in a suspend function?

A

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.

22
Q

How does Continuation help when a function encounters a suspend function?

A

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.

23
Q

How is execution resumed in a coroutine using Continuation?

A

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.

24
Q

What is withContext in Kotlin Coroutines?

A

It’s used to switch the coroutine’s context instead of launching a new coroutine.

25
Q

What is CoroutineContext in Kotlin Coroutines?

A

It defines the behavior of a coroutine, including the Dispatcher, Job, Name, and ExceptionHandler.

26
Q

What does the Job element in CoroutineContext manage?

A

The Job element manages the coroutine’s lifecycle.

27
Q

What is the purpose of the Name element in CoroutineContext?

A

The Name element is usually used for debugging.

28
Q

What does the ExceptionHandler element in CoroutineContext do?

A

The ExceptionHandler handles uncaught exceptions.

29
Q

How is CoroutineContext structured?

A

CoroutineContext > Element > Dispatcher, Job, Name, ExceptionHandler.

30
Q

What is suspendCoroutine in Kotlin Coroutines?

A

It converts a callback into a suspend function by providing a Continuation to resume the coroutine.

31
Q

What is suspendCancellableCoroutine in Kotlin Coroutines?

A

It is similar to suspendCoroutine, but it provides cancellation support, offering a callback for cleanup.

32
Q

What is runBlocking in Kotlin Coroutines?

A

It’s a coroutine builder that blocks the thread until all coroutines inside it complete.

33
Q

What is Job in Kotlin Coroutines?

A

It’s used to control and manage the lifecycle of a coroutine.

34
Q

What is StandardTestDispatcher in Kotlin Coroutines?

A

New coroutines are queued and executed only when the thread is free to use. You need to yield the thread to run enqueued coroutines using functions like advanceTimeBy() method. It’s usually used when you want to focus on concurrency.

35
Q

What is UnconfinedTestDispatcher in Kotlin Coroutines?

A

New coroutines run immediately. If a coroutine suspends, other coroutines resume execution. It’s usually used when concurrency is not required.

36
Q

What is the difference between delay and Thread.sleep in Kotlin?

A
  • delay is a suspend function that pauses the coroutine without blocking the current thread.
  • Thread.sleep is a regular function that blocks the current thread.
37
Q

What is TestDispatcher in Kotlin?

A

TestDispatcher provides a way to control the execution of coroutines during testing, ensuring that coroutines are predictable by simulating delays and manually controlling execution.

38
Q

Why might a Coroutine not be canceled?

A

Cancellation only changes the state of the coroutine, but it doesn’t immediately stop the execution.

39
Q

What can prevent a Coroutine from recognizing cancellation?

A

If the coroutine is performing a long-running task, it may not have a chance to check for the cancellation status, meaning cancellation won’t take effect immediately.

40
Q

What is NonCancellable in Coroutines?

A

It’s a special job that allows a coroutine to run code even if the coroutine has been canceled.

41
Q

When is NonCancellable useful?

A

It’s designed for use with withContext and is helpful for cleanup operations.

42
Q

What is Job in Coroutines?

A

Job is used to control and manage the lifecycle of a coroutine.

43
Q

What is SupervisorJob in Coroutines?

A

SupervisorJob is similar to Job, but it ensures that if one coroutine fails, it doesn’t cancel its sibling coroutines, making it useful for managing independent tasks.

44
Q

What is Deferred in Coroutines?

A

Deferred is a type of Job with a result, created by the async coroutine builder.

45
Q

How can you retrieve the result of a Deferred?

A

The result can be retrieved using the await() method, which suspends the coroutine until the computation is complete.

46
Q

What happens if a Deferred coroutine fails?

A

If the coroutine fails, the await() method will throw an exception.

47
Q

What is launch in Coroutines?

A

launch is used to launch a coroutine and it returns a reference to the coroutine as a Job.

48
Q

What is async in Coroutines?

A

async is used to launch a coroutine and it returns its future result as a Deferred