Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Job and Deferred in Coroutines Kotlin

I am new to coroutines, I understand launch and async but still confusing part is Deferred. What is Deferred? and difference between Job and Deferred. Clear explanation and example is more helpful. Thanks in advance.

like image 231
Magesh Pandian Avatar asked Nov 22 '18 09:11

Magesh Pandian


People also ask

What is Deferred in coroutine?

async starts a new coroutine and returns a Deferred object. Deferred represents a concept known by other names such as Future or Promise : it stores a computation, but it defers the moment you get the final result; it promises the result sometime in the future.

What are jobs in coroutines?

Definition of Job A Job is a cancellable thing with a life-cycle that culminates in its completion. Coroutine job is created with launch coroutine builder. It runs a specified block of code and completes on completion of this block.

What is the difference between launch join and async await in Kotlin coroutines?

Get started with coroutines Well, there is a bit of difference between both, launch does not carry resulting value, it just returns job which can be canceled later, however async returns a Deferred which is a lightweight non-blocking future that represents a promise to provide result later.

What is difference between thread and coroutines?

Coroutines are very similar to threads. However, coroutines are cooperatively multitasked, whereas threads are typically preemptively multitasked. Coroutines provide concurrency but not parallelism.


2 Answers

So job is sort of an object that represents a coroutine's execution and is related to structured concurrency, e.g. you can cancel a job, and all the children of this job will be also cancelled.

From docs:

Job is a cancellable thing with a life-cycle that culminates in its completion.

Deferred is some kind of analog of Future in Java: in encapsulates an operation that will be finished at some point in future after it's initialization. But is also related to coroutines in Kotlin.

From documentation:

Deferred value is a non-blocking cancellable future — it is a Job that has a result.

So, Deferred is a Job that has a result:

A deferred value is a Job. A job in the coroutineContext of async builder represents the coroutine itself.

An example:

someScope.launch {     val userJob: Deferred<User> = async(IO) { repository.getUser(id) }     //some operations, while user is being retrieved      val user = userJob.await() //here coroutine will be suspended for a while, and the method `await` is available only from `Deferred` interface     //do the job with retrieved user } 

Also, it is possible to structure this async request with an existing scope, but that is a talk of another question.

like image 84
nyarian Avatar answered Sep 23 '22 18:09

nyarian


On a basic level, Deferred is a future. It makes it possible for one coroutine to wait for the result produced by another one, suspending itself until it's ready. Calling async is one way, but by far not the only way, to get a Deferred.

However, I think your question is more about the basics: when to use launch, when to use async-await. Here's an important lesson: you probably don't need async. People tend to use it because the keywords async and await are familiar from other languages, but in Kotlin, async is not a general-purpose tool to achieve non-blocking calls.

Here's a basic recipe on how to turn a blocking call into a suspending, non-blocking one:

uiScope.launch {     val ioResult = withContext(Dispatchers.IO) { blockingIOCall() }     ... just use the result, you're on the GUI thread here. } 
like image 44
Marko Topolnik Avatar answered Sep 23 '22 18:09

Marko Topolnik