stepango
03/11/2018, 3:56 PMfun main(args: Array<String>) {
val context = newSingleThreadContext("test")
launch(context) {
withContext(context) {
CompletableFuture.runAsync {
print("Hello")
}.await()
}
}
}
suspend fun <T> CompletableFuture<T>.await() = suspendCoroutine<T> { cont ->
cont.resume(join())
}
Not working😮Jonathan
03/11/2018, 3:57 PMstepango
03/11/2018, 3:58 PMJonathan
03/11/2018, 3:59 PMfun main(args: Array<String>) = runBlocking {
val context = newSingleThreadContext("test")
launch(context) {
withContext(context) {
CompletableFuture.runAsync {
print("Hello")
}.await()
}
}.join()
}
Jonathan
03/11/2018, 3:59 PMCompletableFuture.await
in kotlinx.coroutines-jdk8
Jonathan
03/11/2018, 4:00 PMjoin
you can use parent-child relation:
fun main(args: Array<String>) = runBlocking {
val context = newSingleThreadContext("test")
launch(coroutineContext + context) {
withContext(context) {
CompletableFuture.runAsync {
print("Hello")
}.await()
}
}
}
Jonathan
03/11/2018, 4:01 PMstepango
03/11/2018, 4:28 PMJonathan
03/11/2018, 4:54 PMilya.gorbunov
03/11/2018, 9:54 PMstepango
03/12/2018, 4:17 AMrunBlocking
I just need right context to this construction to make launch + withContext
run synchronously, thats it😊 So far Unconfined
works as I expect, but is there any other context that would be more suitable for this?Jonathan
03/12/2018, 7:58 AMrunBlocking
? This function can be called from anywhere and anything can be called in the lambda. And even if runBlocking
should generally be avoided, it is a first class choice when it comes to testing.stepango
03/12/2018, 8:02 AMfun test(){
smth(coroutineContext) // Unit
//^not suspend function i can't change contains launch + withContext
}
I want to change coroutine context in the way that will make smth
function to run synchronouslystepango
03/12/2018, 8:02 AMJonathan
03/12/2018, 9:20 AMfun test() {
newSingleThreadContext("test context").use { context ->
runBlocking(context) {
smth(context) // Unit
// no need to suspend
// no need to change launch + withContext
}
}
}
Jonathan
03/12/2018, 9:24 AMrunBlocking
doesn't help.