is coroutines are thread based or async/await base...
# announcements
c
is coroutines are thread based or async/await based? as its coroutines so there is cooperation over routines but i can achieve concurrency via single thread similar to nodejs.
l
None. Coroutines are suspending functions. That means they can suspend their executiong, and resume later, functioning the same as if there was the callback, but instead of polluting your code with callbacks, you just call the suspending function, like a regular one. You can achieve concurrency with only one thread, coroutines work in Kotlin/JS, and cancellation is supported there as well, so long the suspending function is cooperative.
c
how does it handle multiple threads?
who handles thread?
d
Answering maieuticly. When you have a function with a callback; how does it handle multiple threads?
c
as far i understand a single thread can have n number of co-routines , in other words n number of callback for the fun if for example you want to handle multiple request (where intensive crypto are done for process) and assigning each thread a request makes sense rather via callbacks and a single thread.
s
How does it handle multiple threads
Coroutines are more like async await (with Structured Concurrency). The fact that they run on threads, fibers, whatever, is somewhat immaterial. Code in a Coroutine is guaranteed to run sequentially, even if the continuation, after a suspending function resumes, is run on a different thread in the Dispatcher's thread pool (if threads are used).
Also, but this is nitpicking, a single-threaded (thread pool with only one thread available at all times) Dispatcher will run Coroutines asynchronously, not concurrently 😀
c
isnt concurrency achieved via async here
btw is dispatcher are executors?
s
Yes, they are like executors on the JVM. Thread pool being dispatched messages/tasks on a queue
Yup, concurrency is done via async, but if your Dispatcher has only one thread, it only will be asynchronous, not concurrent.
I see Coroutines a bit like moving the code from a callback that appears on the right side of a function call to the left side of the function call allowing you to code sequentially/imperatively. 😀
c
there is difference b/w concurrent and parallel , afaik concurrency is achieved via async and its concurrent
d
it only will be asynchronous, not concurrent
Are you sure about this?
I though concurrency doesn't require multiple threads. As you can still have race conditions when you have multiple coroutines running on a single thread.
s
I mixed it up again 😀 Asynchrony is programming model Concurrency is how that is executed (parallel vs concurrent) Single/multi threaded is environment of task execution
When all your Coroutines run on one thread, no race conditions can happen, there'd be no need, for example, to ever put variable access in synchronized blocks. And for Coroutines running on a Dispatcher with multiple threads, the local stack frame (not the heap) is safe from race conditions as well, since sequentiality is guaranteed.
d
Rough example but code like this should still be avoided even in single-threaded contexts.
Copy code
var number: Int = 5
coroutineScope {
  repeat(2) {
    launch {
      val temp = number
      val inc = suspendingHttpCallForInc(temp)
      number = temp + inc
    }
  }
}
s
Yup, but you can put the repeat inside the launch, even if suspendingCallForInc would resume on a different thread from the Dispatcher's thread pool.