What happens if I call some method from an `async`...
# coroutines
m
What happens if I call some method from an
async
block and inside of this method I use
synchronized
block? Will it work or I have to use
Mutex
as stated here https://kotlinlang.org/docs/reference/coroutines/shared-mutable-state-and-concurrency.html#mutual-exclusion ?
g
synchronized may block the thread
it may be a problem, but not necessary, depending what synchronised code does
m
So it’ll work with coroutines just like with normal threads?
g
Coroutines use normal threads
no difference
only problme that with coroutines (suspend funcions) you always expect that this function will not block your thread. synchronised block may be very fast and not a problem, but again, as I said above, it depends what your synchronise block does
if you expect that synchronised block may block for long enough time, than better to use Mutex instead, or as simple solution, just wrap this function to IO dispatcher, so even if it block it will not be a problem for calling thread (especially if it’s Main thread)
m
Thank you Andrey, that is very helpful!