kotlin-coroutines

Difference between thread and coroutine in Kotlin

百般思念 提交于 2019-11-29 19:25:45
Is there any specific language implementation in Kotlin, which differs it from another languages implementation of coroutines? What means that coroutine is like light-weight thread? What is the difference? Are kotlin coroutines actually running in parallely / concurrently? Even in multi-core system, there is only one coroutine running at any given time (is it right?) Here I'm starting 100000 coroutines, what happens behind this code? for(i in 0..100000){ async(CommonPool){ //run long running operations } } Ruslan Since I used coroutines only on JVM, I will talk about JVM backend, there are

Room Dao LiveData as return type causing compile time error

一世执手 提交于 2019-11-29 14:18:51
I am using Room and implemented Dao that returns LiveData . It was working fine with below dependency added. implementation "androidx.room:room-runtime:2.1.0-alpha04" kapt "androidx.room:room-compiler:2.1.0-alpha04" But when I added new Room coroutine dependency as mentioned below. implementation "androidx.room:room-runtime:2.1.0-alpha04" implementation "androidx.room:room-coroutines:2.1.0-alpha04" kapt "androidx.room:room-compiler:2.1.0-alpha04" Below is code which compiles @Dao interface AccountDao{ @Query("SELECT * FROM account_master") suspend fun getAllAccounts(): List<Account> } Below is

Call Kotlin suspend function in Java class

故事扮演 提交于 2019-11-29 07:21:59
问题 Assume we have the following suspend function: suspend fun doSomething(): List<MyClass> { ... } If I want to call this function in one of my existing Java classes (which I'm not able to convert to Kotlin for now) and get its return value I have to provide a Continuation<? super List<MyClass>> as its parameter (Obviously). My question is, How can I implement one. Specially its getContext getter. 回答1: First, add org.jetbrains.kotlinx:kotlinx-coroutines-jdk8 module to your dependencies. In your

Implementing coroutines in Java

馋奶兔 提交于 2019-11-28 15:27:49
This question is related to my question on existing coroutine implementations in Java . If, as I suspect, it turns out that there is no full implementation of coroutines currently available in Java, what would be required to implement them? As I said in that question, I know about the following: You can implement "coroutines" as threads/thread pools behind the scenes. You can do tricksy things with JVM bytecode behind the scenes to make coroutines possible. The so-called "Da Vinci Machine" JVM implementation has primitives that make coroutines doable without bytecode manipulation. There are

Kotlin: withContext() vs Async-await

我怕爱的太早我们不能终老 提交于 2019-11-28 15:22:35
问题 I have been kotlin docs and if I understood correctly the two kotlin functions work as follows : withContext(context) : switches the context of the current coroutine, when the given block executes, the coroutine switches back to previous context. async(context) : Starts a new coroutine in the given context and if we call .await() on the returned Deferred task, it will suspends the calling coroutine and resume when the block executing inside the spawned coroutine returns. Now for the following

Difference between thread and coroutine in Kotlin

大憨熊 提交于 2019-11-28 14:36:50
问题 Is there any specific language implementation in Kotlin, which differs it from another languages implementation of coroutines? What means that coroutine is like light-weight thread? What is the difference? Are kotlin coroutines actually running in parallely / concurrently? Even in multi-core system, there is only one coroutine running at any given time (is it right?) Here I'm starting 100000 coroutines, what happens behind this code? for(i in 0..100000){ async(CommonPool){ //run long running

Room Dao LiveData as return type causing compile time error

蹲街弑〆低调 提交于 2019-11-27 23:59:59
问题 I am using Room and implemented Dao that returns LiveData . It was working fine with below dependency added. implementation "androidx.room:room-runtime:2.1.0-alpha04" kapt "androidx.room:room-compiler:2.1.0-alpha04" But when I added new Room coroutine dependency as mentioned below. implementation "androidx.room:room-runtime:2.1.0-alpha04" implementation "androidx.room:room-coroutines:2.1.0-alpha04" kapt "androidx.room:room-compiler:2.1.0-alpha04" Below is code which compiles @Dao interface

What does suspend function mean in Kotlin Coroutine

╄→尐↘猪︶ㄣ 提交于 2019-11-27 18:42:47
I'm reading Kotlin Coroutine and know that it is based on suspend function. But what does suspend mean? Coroutine or function gets suspended? From https://kotlinlang.org/docs/reference/coroutines.html Basically, coroutines are computations that can be suspended without blocking a thread I heard people often say "suspend function". But I think it is the coroutine who gets suspended because it is waiting for the function to finished? "suspend" usually means "cease operation", in this case the coroutine is idle. 🤔 Should we say the coroutine is suspended ? Which coroutine gets suspended? From

Can “experimental” Kotlin coroutines be used in production?

无人久伴 提交于 2019-11-27 18:39:17
Can Kotlin coroutines be used in production, and what does their experimental status mean? UPDATE : Kotlin coroutines are no longer experimental as of Kotlin 1.3. Kotlin coroutines can and should be used in production. That was the chief reason to officially release them in Kotlin 1.1. Having released them, the JetBrains team had committed to maintain backwards compatibility with respect to any changes that are introduced to them in the minor releases as they evolve, while allowing people to safely try them in complex production applications. In short, the difference between “experimental” and

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

对着背影说爱祢 提交于 2019-11-27 17:07:21
In the kotlinx.coroutines library you can start new coroutine using either launch (with join ) or async (with await ). What is the difference between them? launch is used to fire and forget coroutine . It is like starting a new thread. If the code inside the launch terminates with exception, then it is treated like uncaught exception in a thread -- usually printed to stderr in backend JVM applications and crashes Android applications. join is used to wait for completion of the launched coroutine and it does not propagate its exception. However, a crashed child coroutine cancels its parent with