https://kotlinlang.org logo
#announcements
Title
# announcements
f

Fré Dumazy

11/14/2017, 4:19 PM
okay, I overlooked something important, I'm using coroutines in the method. So
synchronized
worked as it should. But can I use it in
async(UI) { }
or make sure this block isn't executed in parallel
k

karelpeeters

11/14/2017, 4:25 PM
What's wrong with using
syncronized
for that too?
f

Fré Dumazy

11/15/2017, 11:09 AM
After calling a suspend function in a
synchronized
block within a coroutine builder such as
launch { }
or
async { }
, you can (or always) get an
IllegalMonitorStateException
. I think this is because the coroutine might be continued on another thread, while the monitor provided in
synchronized
is on the initial thread. The fix here was to use a
Mutex
object, pass it to the coroutine and replace
synchronized(monitor) { }
by
mutex.withLock { }
6 Views