elizarov
11/14/2017, 4:18 PMPaul Woitaschek
11/14/2017, 4:19 PMPaul Woitaschek
11/14/2017, 4:19 PMPaul Woitaschek
11/14/2017, 4:20 PMelizarov
11/14/2017, 4:22 PMelizarov
11/14/2017, 4:23 PMval ready = ConflatedBroadcastChannel<Boolean>(false)
fun onConnected() { ready.offer(true) }
fun onDisconnected() { ready.offer(false) }
suspend fun waitReady() = ready.consumeEach { if (it) return }
Paul Woitaschek
11/14/2017, 4:25 PMFré Dumazy
11/14/2017, 4:26 PMsynchronized(lock)
inside a async(UI)
block, but this throws an exception java.lang.IllegalStateException: Already resumed, but got exception java.lang.IllegalMonitorStateException: did not lock monitor on object of type 'java.lang.Object' before unlocking
Is there another way that should be used to lock async
blocks?gildor
11/15/2017, 7:58 AMHow to make the 100 iteration execute parallel?@sailxjx To run 100 iterations in parallel you need 100 threads. but CommonPool is just
availableCpu - 1
, don’t know your particular case but in most real cases you don’t want to run 100 threadsgildor
11/15/2017, 7:59 AMI mean maybe I will use some libraries in coroutines, but these libraries may containDon’t understand your questionThread.sleep
gildor
11/15/2017, 7:59 AMsailxjx
11/15/2017, 8:02 AMThread.sleep
in sub functions. They worked well and parallel in threads, but when use launch
, these Thread.sleep
may cause some functions delaysailxjx
11/15/2017, 8:03 AMThead.sleep
to delay
, but when these `Thread.sleep`s are in a third-party library, I can do nothing with it.sailxjx
11/15/2017, 8:05 AMclass MyRunner(val n: Int) {
fun run() {
doSomething()
Thread.sleep(1000)
}
}
fun main(args: Array<String>) {
(1..100).forEach { n ->
thread {
MyRunner(n).run()
}
}
}
gildor
11/15/2017, 8:06 AMgildor
11/15/2017, 8:06 AMsailxjx
11/15/2017, 8:08 AMclass MyRunner(val n: Int) {
suspend fun run() {
doSomething()
// delay(1000) // works as expected
Thread.sleep(1000) // unexpected
}
}
fun main(args: Array<String>) {
(1..100).forEach { n ->
launch(CommonPool) {
MyRunner(n).run()
}
}
Thread.sleep(10000)
}
gildor
11/15/2017, 8:08 AMworks as expected
and unexpected
?gildor
11/15/2017, 8:09 AMavailableCpuCount - 1
threads, so you can run only few threads in parallel. It’s bad to use CommonPool for locking operations, better to have separate Coroutine dispatcher for thatsailxjx
11/15/2017, 8:10 AMdelay(1000)
, the code will finish in 1s. But use sleep
, the code may execute in a long timegildor
11/15/2017, 8:10 AMgildor
11/15/2017, 8:10 AMgildor
11/15/2017, 8:11 AMsailxjx
11/15/2017, 8:11 AMgildor
11/15/2017, 8:11 AMsailxjx
11/15/2017, 8:11 AMsleep
into delay
?gildor
11/15/2017, 8:11 AMgildor
11/15/2017, 8:12 AMsailxjx
11/15/2017, 8:14 AMsleep
to avoid OOM.gildor
11/15/2017, 8:15 AM