Jimmy Alvarez
05/22/2021, 11:04 PMstreetsofboston
05/22/2021, 11:39 PMJimmy Alvarez
05/22/2021, 11:48 PMstreetsofboston
05/23/2021, 12:12 AMJimmy Alvarez
05/23/2021, 12:42 AMscope.launch(onlyOneThreadDispatcher) {
suspendFunc1()
suspendFunc1()
}
This will be blocking, does not will?ephemient
05/23/2021, 2:17 AMlaunch { suspend1() }; launch { suspend2()
may run interleaved at suspend points even if the executor only has one thread. that's kinda the whole point?streetsofboston
05/23/2021, 3:49 AMscope.launch { suspendFunc1() }
scope.launch { suspendFunc2() }
More like the above.
The calls to launch return immediately and the two suspendFuncs run asynchronously.
In your example, Jimmy, where they are called inside the same one launch call, they run sequentially.
But, sequentially or asynchronously, they are never blocking. They are suspending. Unless you put actual blocking code inside of them (eg Thread.sleep() or waiting for a socket, etc)diesieben07
05/23/2021, 9:12 AMlaunch
starts a new coroutine. So technically the answer is: No, one coroutine only ever executes one suspending function at a time. However coroutines are cheap, so you can just launch as many as you need.Erik
05/23/2021, 3:55 PMJimmy Alvarez
05/24/2021, 2:32 PMsuspendFunc1()
function is a remote call, in my understanding suspendFunc2()
will not be executed until remote call completes, even if it is suspend
So, even if the function is suspend it is blocking the coroutine, isn’t it?Jimmy Alvarez
05/24/2021, 2:33 PMErik
05/24/2021, 3:55 PMsuspend
modifier, it indicates that the function might suspend while running (e.g. of it is awaiting results from a network call). A good implementation will not block, but suspend execution, which will later resume.Erik
05/24/2021, 3:59 PMJimmy Alvarez
05/24/2021, 4:52 PMJimmy Alvarez
05/24/2021, 4:52 PM