ron
01/14/2017, 6:52 AMelizarov
01/14/2017, 6:52 AMkotlinx.coroutines
there will be non-blocking sleep, so instead of writing code with callbacks you can just do:
asyncRun {
for (i in 10 downTo 0) {
myLabel.text = "Countdown $i"
sleep(1000)
}
}
And you're text label will count down from 10 to 0 every second. You can do animations with imperative code, too.ron
01/14/2017, 6:53 AMron
01/14/2017, 6:53 AMelizarov
01/14/2017, 6:54 AMasyncRun(myComputePool) {
while (true) {
// some heavy computation in compute thread pool
asyncRun(JavaFx) { myLabel.text = "current progress is $x" }
}
}
So, basically asyncRun(JavaFx) { ... }
is like a Platform.runLater
ron
01/14/2017, 6:54 AMelizarov
01/14/2017, 6:55 AMasyncRun
in application thread it stays in application thread by dispathing its continuations onto application thread with Platform.runLater
ron
01/14/2017, 6:56 AMelizarov
01/14/2017, 6:56 AMron
01/14/2017, 6:56 AMelizarov
01/14/2017, 6:57 AMThread.sleep(...)
inside a coroutine. We'll have an inspection to warn you. Thread.sleep
is thread blocking. You'll use a non-blocking suspend fun sleep(...)
ron
01/14/2017, 6:57 AMron
01/14/2017, 6:57 AMelizarov
01/14/2017, 6:58 AMelizarov
01/14/2017, 7:00 AMasyncRun {
title = "Doing some work"
for (i in 1..10) {
message = "Working $i..."
if (i == 5)
title = "Dome something else"
sleep(200) // you can also do all kinds of non-blocking IO here
progress = i.toLong()
}
}
ron
01/14/2017, 7:01 AMelizarov
01/14/2017, 7:02 AMrunAsync
vs asyncRun
is confusing... The Tornado's runAsync
does start a new thread, but runAsync
does not. It just creates a new coroutine in the same thread, cooperatively dispatching all computation onto it (at least, that is the planned default behavior)ron
01/14/2017, 7:03 AMelizarov
01/14/2017, 7:03 AMron
01/14/2017, 7:03 AMasyncRun { code }
I will expect the code
to be in async completelyelizarov
01/14/2017, 7:05 AMron
01/14/2017, 7:06 AMelizarov
01/14/2017, 7:07 AMron
01/14/2017, 7:07 AMelizarov
01/14/2017, 7:08 AMPlatfrom.runLater(...)
, so for JavaFX I have JavaFx
dispather. Every time coroutine resumes, this dispatcher does Platfrom.runLater
with its continuation, so the rest of its code is back into application thread. Same for Swing and Androidron
01/14/2017, 7:09 AMrunLater
for each resume?elizarov
01/14/2017, 7:10 AMJavaFx
dispatcher, the it does Platform.runLater
. If you run it with Swing
dispatcher, it does SwingUtilities.invokeLater
and so on.elizarov
01/14/2017, 7:11 AMron
01/14/2017, 7:11 AM