Fré Dumazy
11/14/2017, 4:19 PMsynchronized
worked as it should. But can I use it in async(UI) { }
or make sure this block isn't executed in parallelkarelpeeters
11/14/2017, 4:25 PMsyncronized
for that too?Fré Dumazy
11/15/2017, 11:09 AMsynchronized
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 { }