okay, I overlooked something important, I'm using ...
# announcements
f
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
What's wrong with using
syncronized
for that too?
f
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 { }