https://kotlinlang.org logo
#coroutines
Title
# coroutines
c

coder82

10/29/2020, 1:12 PM
Something weird happening here, I launch a coroutine on a single thread context and the profiler shows me it creates a thread, runs the coroutine and puts that thread in WAIT, so my app is leaking threads?
but I suspect here is the culprit
Copy code
override val coroutineContext:CoroutineContext
    get() = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
every time i use coroutineContext
it creates a new one? -_-
Copy code
override fun invokeLater(vararg params:Any, block:suspend AdapterProcess.() -> Unit):Job {
    currentJob = launch(coroutineContext) {
        this@DummyProcess.block()
    }
    return currentJob
}
it must definitely be what is going on, what do you reckon?
m

mateusz.kwiecinski

10/29/2020, 1:21 PM
each time you access
coroutineContext
field you invoke getter
get()
hence you create new singleThreadExecutor on each invocation.
Copy code
override val coroutineContext:CoroutineContext = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
☝️ this would assign created dispatcher to a field once and read its value later
c

coder82

10/29/2020, 1:24 PM
yes indeed now the profiler shows me always the same thread
crazy... this has been lurking in my code since forever
problem solved, when I wrote that line I must have been drunk, really.
😄 4
rubber duck 1
4 Views