uli
04/07/2017, 6:46 AMfun inBackground(doWork: suspend () -> Unit) {
launch(Unconfined) {
try {
doWork()
} catch (t: Throwable) {
// Do Logging, rethrow, ...
}
}
}
// Example showing thread and execution order of inBackground/future combination
// Don't use Thread.sleep in production. Just used to not get distracted by delay() suspension
fun main(args: Array<String>) {
inBackground {
Thread.sleep(100)
println("${Thread.currentThread().name}: before future")
future {
Thread.sleep(100)
println("${Thread.currentThread().name}: in future")
}.await()
Thread.sleep(100)
println("${Thread.currentThread().name}: after future")
}
println("${Thread.currentThread().name}: returned")
Thread.sleep(100000)
}
Which results in:
main: before future
main: returned
ForkJoinPool.commonPool-worker-1: in future
ForkJoinPool.commonPool-worker-1: after future