addamsson
08/18/2019, 10:19 AMrunBlocking {
withContext(someContext) {
// ...
}
}
My problem is that these functions will be called from each other which would lead to nested `runBlocking`/`withContext` calls. I tried how this works here:
runBlocking {
println("In runBlocking 0")
println("Current thread: ${Thread.currentThread().name}")
withContext(Dispatchers.Single) {
println("In withContext 0")
println("Current thread: ${Thread.currentThread().name}")
runBlocking {
println("In runBlocking 1")
println("Current thread: ${Thread.currentThread().name}")
withContext(Dispatchers.Single) {
println("In withContext 1")
println("Current thread: ${Thread.currentThread().name}")
}
}
}
}
but the problem is that here In withContext 1
never gets printed. What is the best practice when one needs to nest these calls but has to keep the API accessible from Java?louiscad
08/18/2019, 10:27 AMwithContext
there? And why do you nest runBlocking
?addamsson
08/18/2019, 10:30 AMwithContext
because I use a single threaded dispatcher to make sure that all calls are executed in sequenceaddamsson
08/18/2019, 10:31 AMaddamsson
08/18/2019, 10:31 AMrunBlocking
is necessary because this will be called from Javaaddamsson
08/18/2019, 10:35 AMaddamsson
08/18/2019, 10:38 AMaddamsson
08/18/2019, 10:38 AMDominaezzz
08/18/2019, 11:50 AMrunBlocking(someContext)
to elide the withContext
.Dominaezzz
08/18/2019, 11:51 AMIn withContext 1
never gets printed because of deadlock.Dominaezzz
08/18/2019, 11:52 AMrunBlocking
in javascript but I can't articulate it.addamsson
08/18/2019, 11:54 AMDominaezzz
08/18/2019, 11:54 AMwithContext
is waiting for the first withContext
to finish.addamsson
08/18/2019, 11:54 AMaddamsson
08/18/2019, 11:55 AMaddamsson
08/18/2019, 11:55 AMDominaezzz
08/18/2019, 11:55 AMaddamsson
08/18/2019, 11:55 AMaddamsson
08/18/2019, 11:56 AMCoroutineScope
and use launch
insteadDominaezzz
08/18/2019, 11:56 AMCompletableFuture<...>
to java (eliminating the need for runBlocking
).addamsson
08/18/2019, 11:56 AMlaunch
within another launch
addamsson
08/18/2019, 11:56 AMaddamsson
08/18/2019, 11:57 AMrunBlocking
is just an actual
functionaddamsson
08/18/2019, 11:57 AMTODO()
in javascript 😄Dominaezzz
08/18/2019, 11:57 AMaddamsson
08/18/2019, 11:57 AMCompletableFuture
?addamsson
08/18/2019, 11:57 AMaddamsson
08/18/2019, 11:58 AMaddamsson
08/18/2019, 11:58 AMDominaezzz
08/18/2019, 11:58 AMtypealias
for CompletableFutute<...>
, Promise<....>
and Deferred<....>
. Although I haven't tried this myself.Dominaezzz
08/18/2019, 11:59 AMi'm trying to avoid creating problems when I call a function which launches a coroutine from another function which launches its ownCan you re-articulate this?
addamsson
08/18/2019, 11:59 AMclass Foo(
override val coroutineContext: CoroutineContext = Dispatchers.Single)
: CoroutineScope {
fun fun0() {
launch {
fun1()
}
}
fun fun1() {
launch {
// ...
}
}
}
addamsson
08/18/2019, 12:00 PMfun0
and fun1
Dominaezzz
08/18/2019, 12:00 PMaddamsson
08/18/2019, 12:00 PMDominaezzz
08/18/2019, 12:00 PMaddamsson
08/18/2019, 12:01 PMDominaezzz
08/18/2019, 12:01 PMFoo
is suspend but the exposed Foo
just calls runBlocking
on the underlying Foo
,.addamsson
08/18/2019, 12:01 PMaddamsson
08/18/2019, 12:02 PMaddamsson
08/18/2019, 12:02 PMaddamsson
08/18/2019, 12:04 PMaddamsson
08/18/2019, 12:04 PMDominaezzz
08/18/2019, 12:04 PMaddamsson
08/18/2019, 12:04 PMaddamsson
08/18/2019, 12:04 PMaddamsson
08/18/2019, 12:05 PMaddamsson
08/18/2019, 12:05 PMMutex
was faster than thread confinementaddamsson
08/18/2019, 12:05 PMactor
function was faster than a Mutex
Dominaezzz
08/18/2019, 12:05 PMactor
is the way to go.addamsson
08/18/2019, 12:06 PMaddamsson
08/18/2019, 12:06 PMaddamsson
08/18/2019, 12:06 PMaddamsson
08/18/2019, 12:07 PMwithContext(coroutineContext) {
withContext(coroutineContext) {
}
}
Dominaezzz
08/18/2019, 12:07 PMaddamsson
08/18/2019, 12:07 PMaddamsson
08/18/2019, 12:07 PMaddamsson
08/18/2019, 12:07 PMDominaezzz
08/18/2019, 12:07 PMwithContext
does a check for similar contexts.addamsson
08/18/2019, 12:08 PMaddamsson
08/18/2019, 12:08 PMaddamsson
08/18/2019, 12:08 PMactor
function anyway?addamsson
08/18/2019, 12:08 PMDominaezzz
08/18/2019, 12:09 PMaddamsson
08/18/2019, 12:11 PMaddamsson
08/18/2019, 12:11 PMaddamsson
08/18/2019, 12:13 PMaddamsson
08/18/2019, 12:13 PMMutex
addamsson
08/18/2019, 12:14 PMDominaezzz
08/18/2019, 12:15 PMaddamsson
08/18/2019, 12:16 PMaddamsson
08/18/2019, 12:16 PMDominaezzz
08/18/2019, 12:21 PMaddamsson
08/18/2019, 12:22 PMaddamsson
08/18/2019, 12:22 PMDominaezzz
08/18/2019, 12:22 PMaddamsson
08/18/2019, 12:23 PMaddamsson
08/18/2019, 12:23 PMaddamsson
08/18/2019, 12:24 PMCompletableFuture
beforeaddamsson
08/18/2019, 12:24 PMaddamsson
08/18/2019, 12:25 PMasync
?Dominaezzz
08/18/2019, 12:26 PMfuture
Dominaezzz
08/18/2019, 12:26 PMGlobalScope.future { ... }
addamsson
08/18/2019, 12:27 PMaddamsson
08/18/2019, 12:27 PMaddamsson
08/18/2019, 12:27 PMaddamsson
08/18/2019, 12:27 PMasync
Dominaezzz
08/18/2019, 12:28 PMkotlinx-coroutines-jdk8
I think.addamsson
08/18/2019, 12:28 PMUnit
addamsson
08/18/2019, 12:28 PMDominaezzz
08/18/2019, 12:28 PMDominaezzz
08/18/2019, 12:28 PMaddamsson
08/18/2019, 12:29 PMaddamsson
08/18/2019, 12:29 PMaddamsson
08/18/2019, 12:30 PMrunBlocking
addamsson
08/18/2019, 12:30 PMlaunch
in all my functions and be done with it but in that case the control is given back to the user without the operation finishedDominaezzz
08/18/2019, 12:31 PMaddamsson
08/18/2019, 12:31 PMDominaezzz
08/18/2019, 12:31 PMaddamsson
08/18/2019, 12:31 PMaddamsson
08/18/2019, 12:33 PMDeferred
which the user can call await
onaddamsson
08/18/2019, 12:34 PMDeferred
is that await
is suspend
so it can't be used from Javaaddamsson
08/18/2019, 12:34 PMDominaezzz
08/18/2019, 12:35 PMDominaezzz
08/18/2019, 12:35 PMaddamsson
08/18/2019, 12:36 PMaddamsson
08/18/2019, 12:37 PMaddamsson
08/18/2019, 12:37 PMDominaezzz
08/18/2019, 12:38 PMDominaezzz
08/18/2019, 12:38 PMCompletableFuture
.addamsson
08/18/2019, 12:38 PMaddamsson
08/18/2019, 12:40 PMawait
functionaddamsson
08/18/2019, 12:40 PMDominaezzz
08/18/2019, 12:40 PMclass Foo : Interface1, Interface2 {
fun stuff() = runBlocking { inten.stuff() }
private class FooInternal {
suspend fun stuff()
}
}
Dominaezzz
08/18/2019, 12:40 PMMutex
.addamsson
08/18/2019, 12:41 PMaddamsson
08/18/2019, 12:41 PMrunBlocking {
mutex.withLock {
state = state.put(key, value)
}
}
Dominaezzz
08/18/2019, 12:42 PMaddamsson
08/18/2019, 12:42 PMaddamsson
08/18/2019, 12:42 PMaddamsson
08/18/2019, 12:42 PMDominaezzz
08/18/2019, 12:42 PMOr maybe use a realI meant a blocking one.Mutex
addamsson
08/18/2019, 12:43 PM@Synchronized
?Dominaezzz
08/18/2019, 12:43 PMaddamsson
08/18/2019, 12:43 PMaddamsson
08/18/2019, 12:44 PMaddamsson
08/18/2019, 12:44 PM@Synchronized
would be just ignoredDominaezzz
08/18/2019, 12:44 PMaddamsson
08/18/2019, 12:44 PMaddamsson
08/18/2019, 12:45 PMaddamsson
08/18/2019, 12:45 PMDominaezzz
08/18/2019, 12:45 PMaddamsson
08/18/2019, 12:45 PMaddamsson
08/18/2019, 12:45 PMDominaezzz
08/18/2019, 12:45 PMaddamsson
08/18/2019, 12:46 PMaddamsson
08/18/2019, 1:24 PM/**
* Marks the JVM method generated from the annotated function as `synchronized`, meaning that the method
* will be protected from concurrent execution by multiple threads by the monitor of the instance (or,
* for static methods, the class) on which the method is defined.
*/
@Target(FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER)
@MustBeDocumented
@OptionalExpectation
public expect annotation class Synchronized()
addamsson
08/18/2019, 1:25 PM@Synchronized
functions in a class only one of them will have access to the monitor, eg: only one of them will be running at any given timeaddamsson
08/18/2019, 1:25 PMDominaezzz
08/18/2019, 1:27 PMaddamsson
08/18/2019, 1:28 PMaddamsson
08/18/2019, 1:28 PMaddamsson
08/18/2019, 1:28 PMaddamsson
08/18/2019, 1:32 PMDominaezzz
08/18/2019, 1:32 PMaddamsson
08/18/2019, 1:52 PMlouiscad
08/18/2019, 5:06 PMaddamsson
08/18/2019, 9:55 PMaddamsson
08/18/2019, 9:56 PMaddamsson
08/18/2019, 9:57 PMaddamsson
08/18/2019, 9:57 PMaddamsson
08/18/2019, 9:57 PMbdawg.io
08/18/2019, 11:05 PMbdawg.io
08/18/2019, 11:06 PMlouiscad
08/19/2019, 6:08 AMaddamsson
08/20/2019, 4:51 PMbdawg.io
08/20/2019, 5:21 PM