xenoterracide
12/14/2018, 5:35 AMgildor
12/14/2018, 7:19 AMgildor
12/14/2018, 7:20 AMgildor
12/14/2018, 7:21 AMgildor
12/14/2018, 7:22 AMin js do they work as workersIn theory it’s possible to use workers (need a way how to serialize coroutine content to run on worker), but no, coroutines on JS are single-threaded and do the same thing as coroutines in JS, allows to do things like:
val result = fetch("...").await()
So provide nice way to work with async codegildor
12/14/2018, 7:24 AMwhat happens when you compile to nativeCurrently coroutines on K/N are also single threaded for now. But milti-threaded will be available at some point https://github.com/Kotlin/kotlinx.coroutines/issues/462
gildor
12/14/2018, 7:27 AMwhat’s the advantage of coroutines over jvm threadsI said that coroutines and threads are very different and it’s true. But you can solve with coroutines problems that you usually solve with threads, like what if you have some IO where most of the time you just wait for result and do nothing. You can just block thread and wait, but because threads are heavy weight (consume a lot of memory, this is sytem calls etc) you waste resources waiting. But coroutines are light weight, it’s just an object, so instead block you can suspend (wait for “callback” result) Also in some cases you just cannot block thread (like in case of UI thread), so you need somehow move job to background you can do this with threads, but getting result back and manage lifecycle is pretty tricky without propper abstraction like RxJava or Kotlinx.Coroutines
xenoterracide
12/14/2018, 6:57 PM