Ad Demo

Dispatchers.Default – Intended for CPU intensive tasks such as sorting data or performing complex
calculations.
The dispatcher is responsible for assigning coroutines to appropriate threads and suspending and resuming the
coroutine during its lifecycle. In addition to the predefined dispatchers, it is also possible to create dispatchers
for your own custom thread pools.
64.6 Coroutine Builders
The coroutine builders bring together all of the components covered so far and actually launch the coroutines so
that they start executing. For this purpose, Kotlin provides the following six builders:
• launch – Starts a coroutine without blocking the current thread and does not return a result to the caller. Use
this builder when calling a suspend function from within a traditional function, and when the results of the
coroutine do not need to be handled (sometimes referred to as “fire and forget” coroutines).
• async – Starts a coroutine and allows the caller to wait for a result using the await() function without blocking
the current thread. Use async when you have multiple coroutines that need to run in parallel. The async
builder can only be used from within another suspend function.
• withContext – Allows a coroutine to be launched in a different context from that used by the parent coroutine.
A coroutine running using the Main context could, for example, launch a child coroutine in the Default
context using this builder. The withContext builder also provides a useful alternative to async when returning
results from a coroutine.
• coroutineScope – The coroutineScope builder is ideal for situations where a suspend function launches
multiple coroutines that will run in parallel and where some action needs to take place only when all the
coroutines complete. If those coroutines are launched using the coroutineScope builder, the calling function
will not return until all child coroutines have completed. When using coroutineScope, a failure in any of the
coroutines will result in the cancellation all other coroutines.
• supervisorScope – Similar to the coroutineScope outlined above, with the exception that a failure in one child
does not result in cancellation of the other coroutines.
• runBlocking – Starts a coroutine and blocks the current thread until the coroutine reaches completion. This is
typically the exact opposite of what is wanted from coroutines but useful for testing code and when integrating
legacy code and libraries. Otherwise to be avoided.
64.7 Jobs
Each call to a coroutine builder such as launch or async returns a Job instance which can, in turn, be used
to track and manage the lifecycle of the corresponding coroutine. Subsequent builder calls from within the
coroutine create new Job instances which will become children of the immediate parent Job forming a parentchild
relationship tree where canceling a parent Job will recursively cancel all its children. Canceling a child does
not, however, cancel the parent, though an uncaught exception within a child created using the launch builder
may result in the cancellation of the parent (this is not the case for children created using the async builder
which encapsulates the exception in the result returned to the parent).
The status of a coroutine can be identified by accessing the isActive, isCompleted and isCancelled properties
of the associated Job object. In addition to these properties, a number of methods are also available on a Job
instance. A Job and all of its children may, for example, be canceled by calling the cancel() method of the Job
object, while a call to the cancelChildren() method will cancel all child coroutines.
The join() method can be called to suspend the coroutine associated with the job until all of its child jobs
have completed. To perform this task and cancel the Job once all child jobs have completed, simply call the

Dispatchers.Default – Intended for CPU intensive tasks such as sorting data or performing complex
calculations.
The dispatcher is responsible for assigning coroutines to appropriate threads and suspending and resuming the
coroutine during its lifecycle. In addition to the predefined dispatchers, it is also possible to create dispatchers
for your own custom thread pools.
64.6 Coroutine Builders
The coroutine builders bring together all of the components covered so far and actually launch the coroutines so
that they start executing. For this purpose, Kotlin provides the following six builders:
• launch – Starts a coroutine without blocking the current thread and does not return a result to the caller. Use
this builder when calling a suspend function from within a traditional function, and when the results of the
coroutine do not need to be handled (sometimes referred to as “fire and forget” coroutines).
• async – Starts a coroutine and allows the caller to wait for a result using the await() function without blocking
the current thread. Use async when you have multiple coroutines that need to run in parallel. The async
builder can only be used from within another suspend function.
• withContext – Allows a coroutine to be launched in a different context from that used by the parent coroutine.
A coroutine running using the Main context could, for example, launch a child coroutine in the Default
context using this builder. The withContext builder also provides a useful alternative to async when returning
results from a coroutine.
• coroutineScope – The coroutineScope builder is ideal for situations where a suspend function launches
multiple coroutines that will run in parallel and where some action needs to take place only when all the
coroutines complete. If those coroutines are launched using the coroutineScope builder, the calling function
will not return until all child coroutines have completed. When using coroutineScope, a failure in any of the
coroutines will result in the cancellation all other coroutines.
• supervisorScope – Similar to the coroutineScope outlined above, with the exception that a failure in one child
does not result in cancellation of the other coroutines.
• runBlocking – Starts a coroutine and blocks the current thread until the coroutine reaches completion. This is
typically the exact opposite of what is wanted from coroutines but useful for testing code and when integrating
legacy code and libraries. Otherwise to be avoided.
64.7 Jobs
Each call to a coroutine builder such as launch or async returns a Job instance which can, in turn, be used
to track and manage the lifecycle of the corresponding coroutine. Subsequent builder calls from within the
coroutine create new Job instances which will become children of the immediate parent Job forming a parentchild
relationship tree where canceling a parent Job will recursively cancel all its children. Canceling a child does
not, however, cancel the parent, though an uncaught exception within a child created using the launch builder
may result in the cancellation of the parent (this is not the case for children created using the async builder
which encapsulates the exception in the result returned to the parent).
The status of a coroutine can be identified by accessing the isActive, isCompleted and isCancelled properties
of the associated Job object. In addition to these properties, a number of methods are also available on a Job
instance. A Job and all of its children may, for example, be canceled by calling the cancel() method of the Job
object, while a call to the cancelChildren() method will cancel all child coroutines.
The join() method can be called to suspend the coroutine associated with the job until all of its child jobs
have completed. To perform this task and cancel the Job once all child jobs have completed, simply call the

Dispatchers.Default – Intended for CPU intensive tasks such as sorting data or performing complex
calculations.
The dispatcher is responsible for assigning coroutines to appropriate threads and suspending and resuming the
coroutine during its lifecycle. In addition to the predefined dispatchers, it is also possible to create dispatchers
for your own custom thread pools.
64.6 Coroutine Builders
The coroutine builders bring together all of the components covered so far and actually launch the coroutines so
that they start executing. For this purpose, Kotlin provides the following six builders:
• launch – Starts a coroutine without blocking the current thread and does not return a result to the caller. Use
this builder when calling a suspend function from within a traditional function, and when the results of the
coroutine do not need to be handled (sometimes referred to as “fire and forget” coroutines).
• async – Starts a coroutine and allows the caller to wait for a result using the await() function without blocking
the current thread. Use async when you have multiple coroutines that need to run in parallel. The async
builder can only be used from within another suspend function.
• withContext – Allows a coroutine to be launched in a different context from that used by the parent coroutine.
A coroutine running using the Main context could, for example, launch a child coroutine in the Default
context using this builder. The withContext builder also provides a useful alternative to async when returning
results from a coroutine.
• coroutineScope – The coroutineScope builder is ideal for situations where a suspend function launches
multiple coroutines that will run in parallel and where some action needs to take place only when all the
coroutines complete. If those coroutines are launched using the coroutineScope builder, the calling function
will not return until all child coroutines have completed. When using coroutineScope, a failure in any of the
coroutines will result in the cancellation all other coroutines.
• supervisorScope – Similar to the coroutineScope outlined above, with the exception that a failure in one child
does not result in cancellation of the other coroutines.
• runBlocking – Starts a coroutine and blocks the current thread until the coroutine reaches completion. This is
typically the exact opposite of what is wanted from coroutines but useful for testing code and when integrating
legacy code and libraries. Otherwise to be avoided.
64.7 Jobs
Each call to a coroutine builder such as launch or async returns a Job instance which can, in turn, be used
to track and manage the lifecycle of the corresponding coroutine. Subsequent builder calls from within the
coroutine create new Job instances which will become children of the immediate parent Job forming a parentchild
relationship tree where canceling a parent Job will recursively cancel all its children. Canceling a child does
not, however, cancel the parent, though an uncaught exception within a child created using the launch builder
may result in the cancellation of the parent (this is not the case for children created using the async builder
which encapsulates the exception in the result returned to the parent).
The status of a coroutine can be identified by accessing the isActive, isCompleted and isCancelled properties
of the associated Job object. In addition to these properties, a number of methods are also available on a Job
instance. A Job and all of its children may, for example, be canceled by calling the cancel() method of the Job
object, while a call to the cancelChildren() method will cancel all child coroutines.
The join() method can be called to suspend the coroutine associated with the job until all of its child jobs
have completed. To perform this task and cancel the Job once all child jobs have completed, simply call the


Categories