Coroutine Is a New Thread

Mahesh Koli
Globant
Published in
3 min readOct 24, 2020

We are going to cover basics of Coroutine in this story.

Let’s have look at it closely 😎

What is Coroutine ?

Kotlin introduced coroutine as recommended solution for asychronous or non blocking programming.

Coroutine is a light-weight thread

The operations such as write/read data to/from database, fetching data from server & long running operations can be carried out smoothly without blocking application UI thread.

In such scenarios coroutine is the saviour.

Let’s see practical use for the same.

Coroutine is introduced in Kotlin but it is not part of Kotlin standard library. It contains in library kotlinx.coroutines.

It is a rich library for coroutines developed by JetBrains. It contains a number of high-level coroutine-enabled primitives.

In order to use coroutines add dependency in your project build.gradle

Add dependency to project build.gradle (app module)

Coroutine Job Creation

Coroutine job is created with following coroutine builder.

  • launch
  • async

It is created with context of some CoroutineScope. We are using GlobalScope context to create Coroutine in below example.

With GlobalScope.launch

Coroutine job created using launch

With GlobalScope.async

Coroutine job created using async

We have created coroutine successfully using launch and async coroutine builder.

By default, the coroutine is immediately scheduled for execution. Other options such as start, block etc can be specified via parameter.

Conceptually, async is just like launch. It starts a separate coroutine which is a light-weight thread that works concurrently with all the other coroutines. The difference is that launch returns a Job and does not carry any resulting value, while async returns a Deferred — a light-weight non-blocking future that represents a promise to provide a result later. You can use .await() on a deferred value to get its eventual result, but Deferred is also a Job, so you can cancel it if needed.

There are some properties and methods of Job which can be use to have control on job.

Job Properties

  • isActive : Returns true when this job is active – it was already started and has not completed nor was cancelled yet. The job that is waiting for its children to complete is still considered to be active if it was not cancelled nor failed.
  • isCancelled : Returns true if this job was cancelled for any reason, either by explicit invocation of cancel or because it had failed or its child or parent was cancelled. In the general case, it does not imply that the job has already completed, because it may still be finishing whatever it was doing and waiting for its children to complete.
  • isCompleted : Returns true when this job has completed for any reason. A job that was cancelled or failed and has finished its execution is also considered complete. Job becomes complete only after all its children complete.

Job Functions

  • cancel : Cancels this job with an optional cancellation cause. A cause can be used to specify an error message or to provide other details on the cancellation reason for debugging purposes. See Job documentation for full explanation of cancellation machinery.
  • invokeOnCompletion : Registers handler that is synchronously invoked once on completion of this job. When the job is already complete, then the handler is immediately invoked with the job’s exception or cancellation cause or null. Otherwise, the handler will be invoked once when this job is complete.
  • join : Suspends the coroutine until this job is complete. This invocation resumes normally (without exception) when the job is complete for any reason and the Job of the invoking coroutine is still active. This function also starts the corresponding coroutine if the Job was still in new state.
  • start : Starts coroutine related to this job (if any) if it was not started yet. The result true if this invocation actually started coroutine or false if it was already started or completed.

Now we know how to create Coroutine and control it .

Enjoy flawless execution of long running operations with coroutine 😎

--

--